Description
This filter is executed before uploading a file (by the File Upload or Post Image fields). It can be used to change the location where uploaded files are stored.
Usage
The following would apply to all forms.
add_filter( 'gform_upload_path', 'your_function_name', 10, 2 );
To limit the scope of your function to a specific form, append the form id to the end of the hook name. (format: gform_upload_path_FORMID)
add_filter( 'gform_upload_path_5', 'your_function_name', 10, 2 );
Parameters
- $path_infoarray
The upload path to be filtered. It is an associative array with two keys which must be specified.
- $form_id integer
The ID of the form currently being processed.
Examples
1. Log the current path
You can use this example when logging is enabled to find out the current path before using the next example to change it.
add_filter( 'gform_upload_path', function ( $path_info, $form_id ) {
GFCommon::log_debug( "log_upload_path(): path_info for form #{$form_id} => " . print_r( $path_info, true ) );
return $path_info;
}, 10, 2 );
2. Change the path and URL
This example changes the upload path and URL for the File Upload field.
add_filter( 'gform_upload_path', 'change_upload_path', 10, 2 );
function change_upload_path( $path_info, $form_id ) {
$path_info['path'] = '/home/public_html/yourdomainfolder/new/path/';
$path_info['url'] = 'http://yourdomainhere.com/new/path/';
return $path_info;
}
Security Considerations
- The default secure “gf-download” links will only work with the default upload path. If you use this filter to modify the path, you cannot use the “gf-download” links, and the file will be directly accessible by filename and URL.
- Gravity Forms will create the necessary directories if they do not exist and the folder permissions allow it. When creating the folders, Gravity Forms will create an empty index.html file to prevent directory browsing and search engine indexing of uploaded files on poorly configured web servers. If you create the folders yourself before using this filter, be sure to disable directory browsing for the folders or place an empty
index.html
file in the directory to prevent directory browsing.
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
This filter is located in GFFormsModel::get_file_upload_path() in forms_model.php.