The gform_fileupload_entry_value_file_path filter allows you to modify the file path of an uploaded file before it is output.
Usage
To use this filter, you can create a custom function and add it to the filter:
add_filter('gform_fileupload_entry_value_file_path', 'your_function_name', 10, 2);
Parameters
- $file_path (string): The file path of the uploaded file.
- $field (GF_Field_Fileupload): The field object for further context.
More information
See Gravity Forms Docs: gform_fileupload_entry_value_file_path
Examples
Change the file path to a custom folder
This example changes the file path to a custom folder named “my_custom_folder”.
function change_file_path($file_path, $field) {
// Replace 'uploads' with 'my_custom_folder'
$new_path = str_replace('uploads', 'my_custom_folder', $file_path);
return $new_path;
}
add_filter('gform_fileupload_entry_value_file_path', 'change_file_path', 10, 2);
Add a prefix to the file name
This example adds a prefix to the file name.
function add_prefix_to_file_name($file_path, $field) {
$path_parts = pathinfo($file_path);
$new_file_path = $path_parts['dirname'] . '/prefix_' . $path_parts['basename'];
return $new_file_path;
}
add_filter('gform_fileupload_entry_value_file_path', 'add_prefix_to_file_name', 10, 2);
Change the file extension to lowercase
This example changes the file extension to lowercase.
function lowercase_file_extension($file_path, $field) {
$path_parts = pathinfo($file_path);
$new_file_path = $path_parts['dirname'] . '/' . $path_parts['filename'] . '.' . strtolower($path_parts['extension']);
return $new_file_path;
}
add_filter('gform_fileupload_entry_value_file_path', 'lowercase_file_extension', 10, 2);
Remove special characters from the file name
This example removes special characters from the file name.
function remove_special_chars_from_file_name($file_path, $field) {
$path_parts = pathinfo($file_path);
$new_file_name = preg_replace('/[^A-Za-z0-9_\-\.]/', '', $path_parts['basename']);
$new_file_path = $path_parts['dirname'] . '/' . $new_file_name;
return $new_file_path;
}
add_filter('gform_fileupload_entry_value_file_path', 'remove_special_chars_from_file_name', 10, 2);
Add a timestamp to the file name
This example adds a timestamp to the file name.
function add_timestamp_to_file_name($file_path, $field) {
$path_parts = pathinfo($file_path);
$timestamp = time();
$new_file_path = $path_parts['dirname'] . '/' . $path_parts['filename'] . '_' . $timestamp . '.' . $path_parts['extension'];
return $new_file_path;
}
add_filter('gform_fileupload_entry_value_file_path', 'add_timestamp_to_file_name', 10, 2);