Skip Navigation

[Resolved] Repeating field: How to show file size for download

This support ticket is created 4 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 5.98 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 9 replies, has 2 voices.

Last updated by Lara 4 years, 1 month ago.

Assisted by: Shane.

Author
Posts
#1796321

Tell us what you are trying to do?

I try to show the file size as a download link for a repeating field. This works fine with a single field. I try to figure out, if it's possible with a repeating field too...

dd_shortcode( 'show-file-size', 'show_file_size_func' );
  
function show_file_size_func( $atts ) {
   extract( shortcode_atts( array('file_url' => '',), $atts ) );
   $arr = explode('/',$url);
   $uploads = wp_upload_dir();
   $upload_path = $uploads['path'];
   return formatSizeUnits(filesize($upload_path.'/'.end($arr)));
}

function formatSizeUnits($bytes)
    {
        if ($bytes >= 1073741824)
        {
            $bytes = number_format($bytes / 1073741824, 2) . ' GB';
        }
        elseif ($bytes >= 1048576)
        {
            $bytes = number_format($bytes / 1048576, 2) . ' MB';
        }
        elseif ($bytes >= 1024)
        {
            $bytes = number_format($bytes / 1024, 2) . ' kB';
        }
        elseif ($bytes > 1)
        {
            $bytes = $bytes . ' bytes';
        }
        elseif ($bytes == 1)
        {
            $bytes = $bytes . ' byte';
        }
        else
        {
            $bytes = '0 bytes';
        }
 
        return $bytes;
}

[show-file-size file_url="[types field='file_field' raw=true][types]"]

Is there any documentation that you are following?
https://toolset.com/forums/topic/how-to-show-file-size-for-download/

Is there a similar example that we can see?
In the documentation - but it's designed for a single field

What is the link to your site?
hidden link

#1796865

I solved the problem with ....

<ul>
[wpv-for-each field="wpcf-rask-bilder"]
  <li data-thumb="[types field='rask-bilder' size='thumbnail' url='true'][/types]" data-src="[types field='rask-bilder' size='full' url='true'][/types]">
    [types field='rask-bilder' title='%%TITLE%%' alt='%%ALT%%' size='full'][/types]
    [show-file-size file_url="[types field='rask-bilder' output='raw' url='true'][/types]"] 
</li>
[/wpv-for-each]
 </ul>

: )

#1797069

I figured out how to set it up with a repeating field with the [wpv-for-each field=""] [/wpv-for-each]
Also it returns the correct file_url path. However I encountered two other problems:

1. Somehow, it doesn't return the correct file size. It always returns "68 kB". No matter what picture I upload and no matter what filesize that picture has...

2. If I click on the "68 kB" it doesn't download...

I changed the code a little, because I found a more elegant way to format the file size with "size_format()". View the code below. It would be awesome, if someone could give me a hint, what could be the source of the problem..

add_shortcode( 'show-file-size', 'show_file_size_func' );
  
function show_file_size_func( $atts ) {
   extract( shortcode_atts( array('file_url' => '',), $atts ) );
   $arr = explode('/',$url);
   $uploads = wp_upload_dir();
   $upload_path = $uploads['path'];
   return size_format(filesize($upload_path.'/'.end($arr)));
}
#1797843

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Lara,

Thank you for getting in touch.

Based on this code i'm not seeing where you are actually checking the URL for the shortcode.

If you want to run the filesize check against the url you will need to pick it out from the $atts array.

Format the code like this.

$atts['file_url'];

Not sure of the purpose of this here $arr = explode('/',$url);

However in order to get the filesize you must get it from the url stored in $atts['file_url'];

Please let me know if this points you in the right direction.
Thanks,
Shane

#1797885

Hi Shane,

many thanks for your answer. According to what the supporter in the documentation said, $arr = explode('/',$url); should get the real path name, not the URL.

ok, I tried your suggestion 🙂

I had to change it into:

add_shortcode( 'show-file-size', 'show_file_size_func' );
  
function show_file_size_func( $atts ) {
   extract( shortcode_atts( array('file_url' => '',), $atts ) );
   $arr = $atts['file_url'];
   return $arr;
}

Now it returns the correct URL, but if I wrap filesize() around it, like that ...

add_shortcode( 'show-file-size', 'show_file_size_func' );
  
function show_file_size_func( $atts ) {
   extract( shortcode_atts( array('file_url' => '',), $atts ) );
   $arr = $atts['file_url'];
   return filesize($arr);
}

.... it returns an error notice, saying that filesize(): stat failed for the URL

Lara 🙂

#1798041

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Lara,

It would seem the filesize() function doesn't work unless you are providing only the filename.

You may be able to use the solution in the link below to get that filesize from the url that is provided by the shortcode using the method in the link below.
https://stackoverflow.com/questions/18073406/get-remote-file-size-using-url-in-php#:~:text=You%20can%20use%20curl_getinfo()%20function%20to%20get%20the%20remote%20file%20size.&text=This%20will%20return%20an%20array,size%20(number%20of%20bytes).

Or this one might be a bit clearer.
hidden link

Please try this and let me know if this helps.
Thanks,
Shane

#1798565
2020-10-03_Filesize-2.PNG
2020-10-03_Filesize.PNG

Thanks Shane 🙂

1. Custom code snippet
-----> Go to Toolset Settings --> Custom Code --> Create New Snipped and enter below code

I modified the code into:

add_shortcode( 'show-file-size', 'show_file_size_func' );
   
function show_file_size_func( $atts ) {
  
extract( shortcode_atts( array('file_url' => '',), $atts ) );
  
//URL of the remote file that you want to get
//the file size of. 
$remoteFile = $atts['file_url'];
  
//Get the header response for the file in question.
$headers = get_headers($remoteFile, 1);

//Convert the array keys to lower case for the sake
//of consistency.
$headers = array_change_key_case($headers);

//Set to -1 by default.
$fileSize = -1;

//Check to see if the content-length key actually exists in
//the array before attempting to access it.
if(isset($headers['content-length'])){
    $fileSize = $headers['content-length'];
}

var_dump($fileSize);
 
//Convert it into KB
$fileSizeKB = round($fileSize / 1024);
 
return 'Download • ' . $fileSizeKB . ' KB';
    
}

It works fine thanks to your suggestions Shane 🙂
As you can see in the pictures, it doesn't output the exact size, but I am fine with it...
<b>May I ask out of curiosity, where deviation can stem from ?</b>

2. Register Shortcode "show-file-size"
-----> Go to Toolset Settings --> Front-end Content ---> Third-party shortcode arguments --> Enter show-file-size in the field "Shortcode name" and click add

3. Use below shortcode in a View or Content Template
a) For Repeating Custom Fields with the following setup ....

<ul class="cu_pferdejornal_bilder">
[wpv-for-each field="wpcf-rask-bilder"]
  <li>
    [types field='rask-bilder' title='%%TITLE%%' alt='%%ALT%%' style='width: 100% !important; height: auto !important;'][/types]
   
    <a href="[types field='rask-bilder' link='true' output='raw'][/types]" target="_blank" rel="noopener noreferrer">[show-file-size file_url="[types field='rask-bilder' output='raw' link='true'][/types]"]</a>
    
[/wpv-for-each]
 </ul>

Replace rask-bilder with our own field-slug. You can find the field-slug in Toolset Custom Fields...

with CSS:
----> Enter in the View / Content Template CSS section:

/*Remove the Bullet Point, when you output the pictures*/
ul.cu_pferdejornal_bilder  {
    list-style-type: none !important;
    margin: 0px !important;
    padding: 0px !important;
}

b) For Single Custom Fields with the following setup ....


[types field='rask-bilder' title='%%TITLE%%' alt='%%ALT%%' style='width: 100% !important; height: auto !important;'][/types]

 <a href="[types field='rask-bilder' link='true' output='raw'][/types]" target="_blank" rel="noopener noreferrer">[show-file-size file_url="[types field='rask-bilder' output='raw' link='true'][/types]"]</a>
    

Replace rask-bilder with our own field-slug. You can find the field-slug in Toolset Custom Fields...

#1798607

The deviation stems from the fact, that it outputs the filesize of the original picture. The user will get access to the original picture as soon as they click the "Download" button. I accidently viewed the filesize of the pictures, that are embedded into the website and because this picture are resized, the filesize shows a deviation ...

#1798903

1. Custom code snippet

The following piece of code need to be removed from the "Custom code snippet", that I shared in the previous post

var_dump($fileSize);

Besides that, it's really a nice solution...

#1798905

My issue is resolved now. Thank you!