Wordpress - How do I get the size of an attachment file?
As far as I know, WordPress has nothing built in for this, I would just do:
filesize( get_attached_file( $attachment->ID ) );
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');
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