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.
// 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!
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.
# 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,
) );
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?
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.
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: