Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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 Aug 19 '10 at 6:44
add comment

3 Answers 3

up vote 18 down vote accepted

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

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

share|improve this answer
    
Ah - that looks much better than messing around with wp_upload_dir() etc.! –  Bobby Jack Aug 18 '10 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 Mar 28 '13 at 11:20
add comment

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');
share|improve this answer
5  
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() –  Brady Oct 27 '11 at 14:01
    
Thanks for the tip, that's a much better way. –  davemac Nov 17 '11 at 1:57
4  
Looks like wp_convert_bytes_to_hr() has now been deprecated in favour of size_format() –  davemac May 14 '12 at 2:35
add comment

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 here: http://codex.wordpress.org/Function_Reference/wp_get_attachment_metadata

Vayu

share|improve this answer
2  
The question is about size of file as in number of bytes, not as in image dimensions. –  Rarst Jul 5 '11 at 14:26
    
Doh, I miss read that. :-) –  Vayu Aug 26 '11 at 12:57
    
thanks mate... saved me time ;) –  Sagive SEO Aug 21 '12 at 17:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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