¿Cómo obtengo el tamaño de un archivo adjunto?

32

Estoy usando el siguiente código de plantilla para mostrar enlaces de adjuntos:

$args = array(
    'post_type' => 'attachment',
    'numberposts' => -1,
    'post_status' => null,
    'post_parent' => $main_post_id
);

$attachments = get_posts($args);

foreach ($attachments as $attachment)
{
    the_attachment_link($attachment->ID, false);
}

pero después del enlace necesito mostrar el tamaño del archivo. ¿Cómo puedo hacer esto?

Supongo que podría determinar la ruta del archivo (a través de wp_upload_dir() y a substr() de wp_get_attachment_url() ) y llamar a filesize() pero eso parece desordenado, y me pregunto si hay un método integrado WordPress.

    
pregunta Bobby Jack 18.08.2010 - 11:11

7 respuestas

38

Por lo que sé, WordPress no tiene nada integrado para esto, simplemente lo haría:

filesize( get_attached_file( $attachment->ID ) );

    
respondido por el Joe Hoyle 18.08.2010 - 11:17
10

He usado esto antes en functions.php para mostrar el tamaño del archivo en un formato fácil de leer:

function getSize($file){
$bytes = filesize($file);
$s = array('b', 'Kb', 'Mb', 'Gb');
$e = floor(log($bytes)/log(1024));
return sprintf('%.2f '.$s[$e], ($bytes/pow(1024, floor($e))));}

Y luego en mi plantilla:

echo getSize('insert reference to file here');
    
respondido por el davemac 19.08.2010 - 05:16
4

Yo haría:

$attachment_filesize = filesize( get_attached_file( $attachment_id ) );

O con un tamaño legible como 423.82 KB

$attachment_filesize = size_format( filesize( get_attached_file( $attachment_id ) ), 2 );

Refs: get_attached_file () , tamaño de archivo () , size_format ()

Nota: Defina su $attachment_id

    
respondido por el l2aelba 27.03.2017 - 13:01
3

Para encontrar el tamaño de un archivo agregado a través del complemento de campos personalizados, hice esto:

$fileObject = get_field( 'file ');
$fileSize   = size_format( filesize( get_attached_file( $fileObject['id'] ) ) );

Solo asegúrese de configurar el "Valor de retorno" del campo personalizado en "Objeto de archivo".

    
respondido por el William Schroeder McKinley 21.08.2015 - 19:33
2

Hay una solución más fácil, para obtener tamaños de archivo legibles por humanos.

$attachment_id  = $attachment->ID;
$attachment_meta = wp_prepare_attachment_for_js($attachment_id);

echo $attachment_meta['filesizeHumanReadable'];
    
respondido por el Zoltan Kiraly 17.01.2017 - 09:39
1

Estaba buscando lo mismo y encontré esta solución integrada de WordPress.

$args = array(
    'post_type' => 'attachment',
    'numberposts' => -1,
    'post_status' => null,
    'post_parent' => $main_post_id
);

$attachments = get_posts($args);

foreach ($attachments as $attachment)
{
    $attachment_id = $attachment->ID;
    $image_metadata = wp_get_attachment_metadata( $attachment_id );
    the_attachment_link($attachment->ID, false);
    echo the_attachment_link['width'];
    echo the_attachment_link['height'];
}

Vea más en wp_get_attachment_metadata()

    
respondido por el Vayu 05.07.2011 - 16:17
1

Al menos para el audio, el tamaño del archivo se guarda como "metadatos".

$metadata = wp_get_attachment_metadata( $attachment_id );
echo $metadata['filesize'];

Este no puede ser el caso de imágenes y video.

    
respondido por el henrywright 27.05.2017 - 15:47

Lea otras preguntas en las etiquetas