40

I'm using the following template code to display attachment links:

$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);
}

but after the link I need to display the file's size. How can I do this?

I'm guessing I could determine the file's path (via wp_upload_dir() and a substr() of wp_get_attachment_url()) and call filesize() but that seems messy, and I'm just wondering if there's a method built into WordPress.

1
  • Interestingly, there is no functionality in the backend to display the size of a file wether in details nor in the list. Ticket #8739
    – hakre
    Commented Aug 19, 2010 at 6:44

9 Answers 9

49

As far as I know, WordPress has nothing built in for this, I would just do:

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

3
  • Ah - that looks much better than messing around with wp_upload_dir() etc.!
    – Bobby Jack
    Commented Aug 18, 2010 at 9:18
  • i need to get only one post attachment's file size. i used get_the_ID() in post_parent. but no use.
    – KarSho
    Commented Mar 28, 2013 at 11:20
  • This is good, but be warned that filesize will throw a warning if get_attached_file returns false. You should add a check to ensure that get_attached_file returns a file that exists. Commented Dec 7, 2019 at 12:53
13

I would do :

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

Or with readable size like 423.82 KB

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

Refs : get_attached_file(), filesize(), size_format()

Note : Define your $attachment_id

10

I have used this before in functions.php to display the file size in an easily readable format:

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))));}

And then in my template:

echo getSize('insert reference to file here');
2
  • 8
    There is no need to create a new function. WP has two of them built into core. size_format() and wp_convert_bytes_to_hr()
    – Scott
    Commented Oct 27, 2011 at 14:01
  • 8
    Looks like wp_convert_bytes_to_hr() has now been deprecated in favour of size_format()
    – davemac
    Commented May 14, 2012 at 2:35
6

There's an easier solution, to get human readable file sizes.

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

echo $attachment_meta['filesizeHumanReadable'];
2
  • 1
    theres a wp_ funktion for everything ;-) Commented Nov 6, 2017 at 9:13
  • 1
    Should be the accepted answer Commented Jan 16, 2019 at 16:34
4

To find the size of a file added through the custom fields plugin, I did this:

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

Just make sure you set the custom field's "Return Value" to "File Object".

0
1

I was looking for the same and found this WordPress built-in solution.

$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'];
}

See more at wp_get_attachment_metadata()

2
  • 3
    The question is about size of file as in number of bytes, not as in image dimensions.
    – Rarst
    Commented Jul 5, 2011 at 14:26
  • Doh, I miss read that. :-)
    – Vayu
    Commented Aug 26, 2011 at 12:57
1

For audio at least, the file size is saved as "metadata".

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

This may not be the case for images and video.

1

Get image file size in wordpress:

$query_images_args = array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'post_status' => 'inherit',
    'posts_per_page' => 10,
);

$query_images = new WP_Query($query_images_args);

foreach ($query_images->posts as $image) {
    $img_atts = wp_get_attachment_image_src($image->ID, $default);
    $img = get_headers($img_atts[0], 1);
    $size = $img["Content-Length"]/1024;
    echo round($size);

}
3
  • Welcome to wpse! How is this approach better then the accepted answer? Consider adding more explanation about how/why this approach is better or how it solves OPs problem. Commented Oct 27, 2021 at 7:42
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Oct 27, 2021 at 7:53
  • This seems far superior, i.e. using get_headers vs. the php filesize function. This get_headers route does not throw the 'stat failed' error if the file size is missing. Commented Jan 21, 2022 at 23:20
0

Simple function to get attachment file size by attachment id, similar to another answer here but with a little error checking. There is also an additional parameter to specify the number of decimal points to return:

function tm_get_attachment_size($attachment_id, $decimals) {
    $file = get_attached_file( $attachment_id );
    return $file ? size_format( filesize($file), $decimals ) : 0;
}

For example, say I have an attachment ID of 99: tm_get_attachment_size(99, 1) would return '1.4 MB'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.