Description
gform_submission_files_pre_save_field_value allows filtering (e.g., renaming) of the submission files before they are saved to the form uploads folder and entry.
Usage
add_filter( 'gform_submission_files_pre_save_field_value', 'my_function', 10, 4 );
Parameters
| Parameter | Type | Description |
|---|---|---|
| $files | array | Array of submission files. See Files Parameters. |
| $field | GF_Field_FileUpload | The File Upload field the files are for. |
| $entry | array | The entry currently being saved. Only fields located before the current field are included. |
| $form | array | The form currently being processed. |
Files Parameters
| Parameter | Type | Description |
|---|---|---|
| new | array | Array of new files from $_FILES. See New File Parameters. |
| existing | array | Array of dynamically populated file URLs and/or files that have already been uploaded to the form tmp folder. See Existing File Parameters. |
New File Parameters
| Parameter | Type | Description |
|---|---|---|
| name | string | The original name of the file on the client machine. |
| type | string | The MIME type of the file provided by the browser. |
| size | integer | The size of the file in bytes. |
| tmp_name | string | The temporary filename of the file in which the uploaded file was stored on the server. |
| error | integer | The PHP error code for the file upload. |
| full_path | string | The full path as submitted by the browser. Only populated with PHP 8.1+. |
Existing File Parameters
| Parameter | Type | Description |
|---|---|---|
| uploaded_filename | string | The name of the uploaded file, an existing file, or the name parsed from the populated URL. |
| temp_filename | string|null | The temporary name of the file. Only present if the file has been saved to the form tmp folder. |
| url | string|null | The file URL. Only present if the file has been dynamically populated on initial form display. |
Examples
Rename uploaded files to include the entry and field IDs.
add_filter( 'gform_submission_files_pre_save_field_value', function ( $files, $field, $entry ) {
$entry_id = absint( rgar( $entry, 'id' ) );
foreach ( $files['existing'] as &$existing_file ) {
$existing_file['uploaded_filename'] = sprintf(
'entry-%d-field-%d-%s',
$entry_id,
$field->id,
$existing_file['uploaded_filename']
);
}
foreach ( $files['new'] as &$new_file ) {
$new_file['name'] = sprintf(
'entry-%d-field-%d-%s',
$entry_id,
$field->id,
$new_file['name']
);
}
return $files;
}, 10, 3 );
Placement
This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.
See also the PHP section in this article: Where Do I Put This Code?
Source Code
The filter is located in GF_Field_FileUpload::filter_submission_files_pre_save() in includes/fields/class-gf-field-fileupload.php
Since
Gravity Forms 2.9.18