Skip Navigation

[Resolved] Short code to allow users to view the uploaded file's name, size and type

This support ticket is created 6 years, 3 months 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 0.01 hours from now. Thank you for your understanding.

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

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by imaneK 6 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#601002

We're currently developing the website using Divi and toolset.

I have created a form for website users to be able to upload files. Once the user has uploaded files, these should be made downloadable for other users. At the moment, I have created a button called 'download now' which downloads each file. However, the file's information is not shown on the button such as the file name, size and type.

I know there is a way of showing this information however it required php code to create a shortcode. There are a few forums that explain how to do this which I have tried to follow but have failed to make it work.

Below is a link to the forums:

https://toolset.com/forums/topic/print-out-file-name-in-view-template/
https://toolset.com/forums/topic/display-file-name-of-uploaded-file-multi-instance/
https://toolset.com/forums/topic/display-the-file-name-of-uploaded-pdf-files-and-the-link-in-a-different-tab/

Links to website:

hidden link

Username: helloworld
Password: *****

Content Template Edit:
hidden link

Thank you for your help and support.

#601036

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - I updated the the password as it was available for all.

You should send me access details now. I have set the next reply to private which means only you and I have access to it.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

You want to display following things for which custom field?:
=> file name - OK
=> size (which size - file size?)
=> type - OK

#601581

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I've adjusted the custom shortcode as given under within your current theme's functions.php file:

add_shortcode( 'my_file_name', 'wpml_hard_link'); // Actually activate the shortcode
function wpml_hard_link($atts) {
    global $post; // So we can get the post meta later on
  
    $url = "{$atts['file_url']}";
     $types = "wpcf-upload-file";
	
			
      
    if ($types) { // if the types_field argument was provided 
  
        $urls = get_post_meta($post->ID,$types); // let's get the (potentially multiple) values
      
        $content = ''; // Setting up a variable to hold the links so we can return it later
      
        foreach ($urls as $fileurl) { // Let's iterate for each of the multiple values
            $arr = explode('/',$fileurl); // Split it up so that we can just grab the end part, the filename
            $filename = explode(".",end($arr));
					
			$content .= '<a href="'.$fileurl.'">'.$filename[0].'</a>'; // Create the link and store it in the $content variable
			
			$content .= ' Type: '.strtoupper($filename[1]);
			$headers = get_headers($fileurl, TRUE);
            $filesize = $headers['Content-Length'];
			
			
	switch ($filesize) {
        case $filesize < 1024:
            $size = $filesize .' B'; break;
        case $filesize < 1048576:
            $size = round($filesize / 1024, 2) .' KiB'; break;
        case $filesize < 1073741824:
            $size = round($filesize / 1048576, 2) . ' MiB'; break;
        case $filesize < 1099511627776:
            $size = round($filesize / 1073741824, 2) . ' GiB'; break;
    }

			
			
			$content .= ' Size: '.$size;
			$content .= '<br />';
			
        }
          
        return $content; // Return the content as the shortcode value
      
    } else {  // Else we didn't use the fields_type argument, we just needed one URL we provided explicitly
            $arr = explode('/',$url); // So let's split that URL up so we can grab the end
            return '<a href="'.$url.'">'.end($arr).'</a>'; // And return the resultant link
      
    } // We're done!
      
}

I've called the shortcode as given under:

[my_file_name]

I can see it working here:
=> hidden link

It's showing - filename, file type and file size.

#602270

Thank you very much for your support.

#622132
Capture1.PNG
Capture.PNG

Hi,
Its seems there warning 'Error' with the code that was added to the website. However, I didn't want to make any changes without knowledge.

I have provided screenshots for more clarity.

Your guidance would be very appreciated.

Thank you.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.