Support

Account

Home Forums General Issues get filesize?

Solved

get filesize?

  • searched the forum but nothing … . i have a “data” field (downloads) and want to display the filesize of the linked file.

    my code:

    <?php $attachment_id = get_field('download');
    $url = wp_get_attachment_url( $attachment_id );
    $title = get_the_title( $attachment_id );
    if( get_field('download') ): ?>
    <a href="<?php echo $url; ?>" >Download File "<?php echo $title; ?>"</a>
    <?php endif; ?>
  • hi elliot, thanks, i know this thread – wordpress tells me how to display the filesize: http://codex.wordpress.org/Function_Reference/size_format – but how do i GET it?

  • Hi @herrfischer

    It all depends on what ‘it’ is.

    Your code above shows that you understand how to get the attachment ID, so reading over the linked thread will show how to use this ID to load the file size.

    Cheers
    E

  • this way i get a size:

    <?php $attachment_id = get_field('download');
    $url = wp_get_attachment_url( $attachment_id );
    $title = get_the_title( $attachment_id );
    
    if (get_field('download')): ?>
    <a href="<?php echo $url; ?>" ><?php echo $title; ?></a>
    
    <?php 
    $file_size = $attachment_id;								echo size_format($file_size, 2);									?>								
    <?php endif; ?>

    but its not the size of the file it’s always about 1kb, i think it’s the size of the “ID” itself? i get the title with “get_the_title” but how do i get the size in MB?

  • finally got it:`
    <?php $attachment_id = get_field('download');
    $url = wp_get_attachment_url( $attachment_id );
    $title = get_the_title( $attachment_id );

    // part where to get the filesize
    $filesize = filesize( get_attached_file( $attachment_id ) );
    $filesize = size_format($filesize, 2);

    // show custom field
    if (get_field('download')): ?>
    <a href=”<?php echo $url; ?>” ><?php echo $title; ?></a>

    <!– show filesize –>
    <?php echo $filesize; ?>
    <?php endif; ?>
    `
    πŸ™‚ <- happy

  • Worked for me, just what I was looking for thanks πŸ™‚

  • This reply has been marked as private.
  • Thanks herrfischer. There should be more people like you in boards about programming stuff.

  • With this you get the extension, too:

    get it:
    $path_info = pathinfo( get_attached_file( $attachment_id ) );

    show it:
    echo $path_info['extension']

    full example with a repeater field (to show some downloads):

    <?php
    // Downloads repeater field
    if (get_row_layout() == 'download_list'): ?>
    <?php if(get_sub_field('download')) { ?>
    <ul class="unstyled downloads">
    <?php while(has_sub_field('download')) {
    $attachment_id = get_sub_field('singledl');
    $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);
    $path_info = pathinfo( get_attached_file( $attachment_id ) ); 
    ?>
    <li class="table">
    <a href="<?php echo $url; ?>" class="download table-row break">
    <span class="icon-download2 iconF icon-20 icon-fl" aria-hidden="true"></span>
    <?php echo '<span class="filetitle table-cell">' . $title . ' (' . $path_info['extension'] . ')</span>' . '<span class="filesize table-cell">' . $filesize . '</span>' ?>
    </a>				
    </li>
    <?php } ?>
    </ul>
    <?php } ?>	
    <?php ?>
  • herrfischer how are you getting the $attachment_id? I’m assuming your download or file is download so what is the field singledl? I’m assuming “single download” but how does that work?

  • sorry i can not check it because i am not at work. if i remember right “download_list” is a flexible content field, “download” is a repeater field and “singledl” is the file field.

    flexible content > repeater > file.

  • Most important here is to set Return Value to β€œFile ID.” πŸ™‚

Viewing 13 posts - 1 through 13 (of 13 total)

The topic ‘get filesize?’ is closed to new replies.