Rename filename on upload to Dropbox

Hi. I am not a big PHP guy or coder. I have some very basic knowledge and I can sometimes piece things together, but I’m banging my head on this. I just don’t understand it.

I’m trying to change the filename of any files uploaded in a specific form to Dropbox using the Dropbox Add-on. I want the files to be ‘TEXT - Date - Last Name, First Name’ without changing the extension. The date and names would be entered from within the specified form.

I’ve found documentation on it here, but I’m just not understanding it. I know it needs to be added to the functions.php file, but I’m struggling with the syntax. Maybe I’m just having a brain-fart.

Any advice or tips would be helpful. Thank you.

It’s going to look something like this:

// TEXT - Date - Last Name, First Name
// apply to form 8 only.  Change 8 here to your form ID
add_filter( 'gform_dropbox_file_name_8', 'change_name', 10, 5 );
function change_name( $file_name, $form, $field_id, $entry, $feed ) {
        $ext         = new SplFileInfo( $file_name );
        $date        = rgar ( $entry, '5' );   // change 5 to the ID of your date field
        $first_name  = rgar ( $entry, '1.3' ); // change 1 to the ID of your name field if using an Advanced name field
        $last_name   = rgar ( $entry, '1.6' ); // change 1 to the ID of your name field if using an Advanced name field
        $filename    = 'TEXT - ' . $date . ' - ' . $last_name . ', ' . $first_name . '-.' . $ext;
        GFCommon::log_debug( __METHOD__ . "(): Returning filename: {$event}." );
        return $filename;
}

You will need to change the form ID and the field IDs, but I believe that will get you close.

First of all, thank you! That is great! Exactly what I’m looking for. It makes sense, I can follow it, and it appears it should work, but it doesn’t. So it must be something else that I’m not thinking of.

I’ll keep looking and see if there is some variable I’m not considering at the moment. Thank you!

Please be sure you enable logging - there is a logging statement in there that should help.

I just realized this is wrong (copy pasta error):

GFCommon::log_debug( __METHOD__ . "(): Returning filename: {$event}." );

Should be

GFCommon::log_debug( __METHOD__ . "(): Returning filename: {$filename}." );

Chris is a beast! Always beats me to the punch… but if you want a more robust solution that might be a little easier to implement, check out the Gravity Wiz snippet that allows you rename upload files with a merge-tag-based template.

https://gravitywiz.com/rename-uploaded-files-for-gravity-form/

To be clear, this will rename the file everywhere whereas Chris’ solution will only rename it in Dropbox.

1 Like

Hi, I’m looking at David’s snippet and am unclear about some of the instructions. For example …

When he says:

  1. Configure the snippet
  • Once you have installed the snippet, find the snippet configuration section located at the bottom of the snippet code.
  • Replace the form_id with the ID of your form.
  • Replace the field_id with the field ID of your File Upload field.
  • Replace the template with your custom file name template.

Here’s the form I’m working on. TEST - Form 2 | Alfandre Architecture

  1. Would it look like this?

     # Configuration
    
     new GW_Rename_Uploaded_Files( array(
     	'form_id'          => 10,
     	'field_id'         => 8,
     	// most merge tags are supported, original file extension is preserved
     	'Any Name I want'         => '{Name (First):1.3}-{Name (Last):1.6}-{filename}',
     	// Ignore extension when renaming files and keep them in sequence (e.g. a.jpg, a1.png, a2.pdf etc.)
     	'ignore_extension' => false,
     ) );
    
  2. Also, there are 5 file upload fields (all 5 not shown in the screenshot) on the form? I believe the config section above is for one field?

  3. Lastly, you can see in the form that the name field used is actually a single-line text field. It’s not a first and last name filed as shown in the configuration section. How would I alter this snippet to accommodate the single-line text field on the form?

Hi Jordan, looking back at the original config snippet:

new GW_Rename_Uploaded_Files( array(
	'form_id'          => 628,
	'field_id'         => 3,
	// most merge tags are supported, original file extension is preserved
	'template'         => '{Name (First):1.3}-{Name (Last):1.6}-{filename}',
	// Ignore extension when renaming files and keep them in sequence (e.g. a.jpg, a1.png, a2.pdf etc.)
	'ignore_extension' => false,
) );

You would have five of those sections, one for each field_id. Change the field_id in each of them. Then, in this line:

	'template'         => '{Name (First):1.3}-{Name (Last):1.6}-{filename}',

You would leave the template alone - that is required for the code. This is what you change:

{Name (First):1.3}-{Name (Last):1.6}-{filename}

In your case, the template line would probably look like this to use the name from field 1 in your renamed file:

	'template'         => '{:1}-{filename}',

If I uploaded a file called ‘puppies.jpg’ and used my name Chris in your name field, the file would be renamed:

Chris-puppies.jpg

after the code ran for the field.

Does that make sense? You would need to have 5 nearly identical sections, except for the field_id.

Thanks, so if I understand you, based on your direction and my form here, the code should look like this?

    # Configuration

    new GW_Rename_Uploaded_Files( array(
        'form_id'         =>10,
        'field_id'         => 3,
        'field_id'         => 13,
        'field_id'         => 15,
        'field_id'         => 17,
        'field_id'         => 10,
    	// most merge tags are supported, original file extension is preserved
    	'template'         => '{:1}-{filename}',
    	// Ignore extension when renaming files and keep them in sequence (e.g. a.jpg, a1.png, a2.pdf etc.)
    	'ignore_extension' => false,
    ) );

I don’t understand why he says “Replace the template with your custom file name template” if that’s not what you’re supposed to do?

Hey @jordydme, sorry for the confusion. In the same way it says to replace the “form_id” with your form ID and you wrote modified the value of the form_id like so:

'form_id' =>  123

You should modify the value of the template parameter rather than the key itself.

'template' => 'my-modified-template-value-that-can-include-most-merge-tags-{filename}'

As for applying this to multiple fields, you would need to create a new instance for each like so:

new GW_Rename_Uploaded_Files( array(
        'form_id'  => 10,
        'field_id' => 3,
    	'template' => '{:1}-{filename}',
) );

new GW_Rename_Uploaded_Files( array(
        'form_id'  => 10,
        'field_id' => 13,
    	'template' => '{:1}-{filename}',
) );

new GW_Rename_Uploaded_Files( array(
        'form_id'  => 10,
        'field_id' => 15,
    	'template' => '{:1}-{filename}',
) );

// etc, add as many instances as you want!

PS - While we try to keep a loose eye on the community forums, we’re vigilant about support on our own site! Feel free to submit a comment with any other questions on the Rename Uploaded Files article:

1 Like

And just like that, the wizard has appeared, waved his wand, and vanished as quickly as he arrived:

1 Like