0

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">
     <?php
       while(have_rows('download')) : the_row();
     ?>
     <li class="custom-ul-class col-md-6 col-sm-12 col-xs-12">
        <span class="file-size">
        <?php
            $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;
         ?>
         </span>
       </li>
       <?php endwhile; ?>
     </ul>
<?php endif; ?>
1

1 Answer 1

1

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.

3
  • It doesn't work. But it returns me "stat failed" error message
    – Zee
    Sep 26, 2015 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, 2015 at 5:44
  • if you pass in the ACF file filesize, make sure you set return format of the acf field to file array, & pass the $attachment_id[’filesize'] to this function
    – Mark
    Feb 21, 2023 at 9:37

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.