I am using ACF plugin and trying to get file size which in a repeater field, but it returns nothing. Here is my code.

<?php if(have_rows('download')) : ?>
  <ul class="file">
       while(have_rows('download')) : the_row();
     <li class="custom-ul-class col-md-6 col-sm-12 col-xs-12">
        <span class="file-size">
            $attachment_id = get_sub_field('file');
            $url = wp_get_attachment_url( $attachment_id );
            $title = get_the_title( $attachment_id );
            $filesize = filesize( get_attached_file( $attachment_id ) );
            $filesize = size_format($filesize, 2);
            echo $filesize;
       <?php endwhile; ?>
<?php endif; ?>

As far as I know, WordPress has nothing built in for this, I would just do. Same thing did you also. So make sure that the $attachment_id having value.

You can use custom function by place that function code in your function.php file.

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

After place above function code, you can use getSize($url); where ever you use into your template. Here $url is reference of your file path.

Let me know if this stuff not help you.

  • It doesn't work. But it returns me "stat failed" error message
    – Zee
    Sep 26 '15 at 19:01
  • Please check $attachment_id should have ID value. If not then please check with $attachment_id = get_field('download');. Other then this your code is correct and it should work.
    – ndlakhs
    Sep 27 '15 at 5:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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