• Resolved michaeledi

    (@michaeledi)


    Hi,
    I’ve noticed that the file uploaded just changen the name on the frontend, but when I try to download it, the real filename is still really long, like “wp-content-uploads-gravity_forms-1-7e38ba7731a19dd489bc64977595aeff-01-2017-OrderFile147.stl”, how can I display only the raw name without adding the prefix?
    Thanks

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author ovann86

    (@ovann86)

    Hey,

    Can you share what filter you’ve applied so far?

    The filename shouldnt be what you’ve shown (that is, the name shouldnt include the path).

    I’m using this filter on my demo site and it’s not having the same issue.

    https://demo.itsupportguides.com/ajax-upload-for-gravity-forms/multi-page-form/

    add_filter( 'itsg_gf_ajaxupload_filename', 'my_itsg_gf_ajaxupload_filename', 10, 7 );
    function my_itsg_gf_ajaxupload_filename( $name, $file_path, $size, $type, $error, $index, $content_range ) {
        $form_id = isset( $_POST['form_id'] ) ? $_POST['form_id'] : null; // get the form_id out of the post
        $field_id = isset( $_POST['field_id'] ) ? $_POST['field_id'] : null; // get the field_id out of the post
        $file_extension = pathinfo( $name, PATHINFO_EXTENSION );  // get the file type out of the URL
    	
    	$form_meta = GFFormsModel::get_form_meta( $form_id ); // load the form meta
    	$field = GFFormsModel::get_field( $form_meta, $field_id ); // get the field
    
        $field_label = GFFormsModel::get_label( $field ); // get the field label
    	$field_label = preg_replace( '/[^A-Za-z0-9 ]/', '', $field_label ); // make sure the field label only includes supported characters -- restricting to A-Z 0-9
    	
        $name = $field_label . '_' . $field_id . '_' . $form_id . '.' . $file_extension; // put it together
    	
        return $name; // now return the new name
    }

    What were you wanting to change the file name to?

    If using a username/user id – will a user always be logged in, what would you use if not logged in.

    Thread Starter michaeledi

    (@michaeledi)

    Hi,
    1. I tried your snippet, and it did change the name displayed in the upload field, but when I click the filename to download it or ‘save as’, there is always the ‘wp-content-uploads-gravity_forms-1-7e38ba7731a19dd489bc64977595aeff-01-2017’ prefix in the downloaded file’s name. And I don’t know what’s wrong…
    2. In my site, there is no chance that a guest can upload the file, all the forms are set to ‘log-in only’, so I think the user_id is available.
    Thanks!

    • This reply was modified 7 years, 11 months ago by michaeledi.
    Thread Starter michaeledi

    (@michaeledi)

    I’m wondering if it’s because I used the CDN, so the domain might affect the downloaded file name?

    Thread Starter michaeledi

    (@michaeledi)

    Hi,
    I confirmed that the problem is caused by CDN, I have to add a suffix ‘?attname=’ to the tail of the file url, then the files will remain short and clean.

    So is there any way to add this suffix automatically?

    Plugin Author ovann86

    (@ovann86)

    I should be able to setup a filter so you can do this, but first just to check – do you mean just adding ?attname= to the end of the file URL or does it need to be ?attname=nameOfFile.txt ?

    Thread Starter michaeledi

    (@michaeledi)

    Just ‘?attname=’ , I have added it in the $column = "<a href='{$file_url}?attname=' target='_blank' >{$file_name_decode}</a>" and it works.

    Is that what you would do? Or you have some soultion that needn’t change the plugin?

    Plugin Author ovann86

    (@ovann86)

    Yea, that’s what I’m aiming for – but I’ll make it so it all happens on the server side of things.

    I’ll hopefully push out an update shortly that’ll support it … but this should be the filter (let me know if you’re not sure what or where to place it).

    add_filter( 'itsg_gf_ajaxupload_response', 'my_itsg_gf_ajaxupload_response', 10, 3 );
    
    function my_itsg_gf_ajaxupload_response( $upload_file, $form_id, $field_id ) {
    	if ( isset( $upload_file['files'][0]->url ) ) {
    		$upload_file['files'][0]->url .= '?attname=';
    	}
    	return $upload_file;
    }
    • This reply was modified 7 years, 10 months ago by ovann86.
    Thread Starter michaeledi

    (@michaeledi)

    Hi Ovann,
    Really appreciate that! But it seems not working… The link didn’t have the suffix after I pasted it in functions.php.

    Thread Starter michaeledi

    (@michaeledi)

    Hi Ovann,
    The filter seems not working… The link didn’t have the suffix after I pasted it in functions.php.

    Plugin Author ovann86

    (@ovann86)

    Ah sorry, I forgot – because the upload doesn’t run a theme your theme’s functions.php is not loaded.

    What you need to do is create a custom plugin, put your code in there and activate the plugin.

    Using a custom plugin like this is MUCH better than modifying your theme’s functions.php file. It means you can change themes or update without loosing the customisations and if you’re having issues you can simply deactivate the ‘custom’ plugin to check if the problem is caused by a customisation.

    Here’s a template custom plugin: https://www.itsupportguides.com/downloads/itsg-custom-functions.zip

    And how I create them: https://www.itsupportguides.com/wordpress/how-to-create-a-wordpress-plugin-for-your-custom-functions/

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to change the filename while downloading?’ is closed to new replies.