Uploaded files containing special characters, like accented characters or glyphs like € and £ of even whitespace characters, can cause al sorts of issues, depending on the configuration of your hosting server. This can cause a lot of headaches when your Gravity Form has an upload field where people can upload files with uncommon characters in the filename.
Luckily there is an easy solution by adding a simple snippet to your themes functions.php. We will explain every line of code, before putting it all together.
How to remove accents from uploaded filenames
For this WordPress had a built-in function we can use: remove_accents
This function replaces characters like À Â Å with A and € with E for example.
// Removes chars with accents etc, also replaces € with E.
$sanitized_filename = remove_accents( $filename );
How to remove unwanted characters from uploaded filenames
To achieve this we we replace all character in the filename with nothing, except for the characters we want to allow. In this case we want to preserve A-Z a-z 0-9 – _ . and space characters. This can be achieved by using PHP function preg_replace which performs a search and replace on a string based on a regular expression.
// Remove every character except A-Z a-z 0-9 . - _ and spaces.
$sanitized_filename = preg_replace( '/[^A-Za-z0-9-_\. ]/', '', $filename );
How to replace spaces in a filename with underscores (or dashes)
Here we also use preg_replace to replace whitespace with an underscore. Note that multiple spaces are replaced by a single underscore.
// Replace spaces (blanks) with an underscore.
$sanitized_filename = preg_replace( '/[[:blank:]]+/', '_', $filename );
If you prefer dashes over underscores use:
// Replace spaces (blanks) with a dash.
$sanitized_filename = preg_replace( '/[[:blank:]]+/', '-', $filename );
Putting it together
You can copy / paste the following code snippet in your WordPress theme’s functions.php. For every file uploaded in WordPress or Gravity Forms upload fields the filename will be cleaned and converted.
/**
* Convert and clean uploaded filenames in WordPress.
* - Remove chars with accents etc, also replaces € with E.
* - Remove every character except A-Z a-z 0-9 . - _ and spaces.
* - Replace spaces (blanks) with an underscore.
*
* @author gravitywp.com
* @param string $filename
* @return string
*/
add_filter( 'sanitize_file_name', 'gwp_sanitize_file_name', 10, 1 );
function gwp_sanitize_file_name( $filename ) {
// Remove chars with accents etc, also replaces € with E.
$sanitized_filename = remove_accents( $filename );
// Remove every character except A-Z a-z 0-9 . - _ and spaces.
$sanitized_filename = preg_replace( '/[^A-Za-z0-9-_\.[:blank:]]/', '', $sanitized_filename );
// Replace spaces (blanks) with an underscore.
$sanitized_filename = preg_replace( '/[[:blank:]]+/', '_', $sanitized_filename );
return $sanitized_filename;
}
Note we added a docblock explaining the code and that we replace $filename with $sanitized_filename in any subsequent line after the first replacement.
Our Premium add-ons for Gravity Forms
List Dropdown
Add a Dropdown Select with choices to a column or multiple columns in a Gravity Forms List Field.
JWT Prefill
Fill forms with data you can trust. Prefill Gravity Forms fields with a secure token instead of links with editable url parameters, so your data is accurate, tamper-proof, and ready to use.
List Number Format
With this Gravity Forms Add-on you can change List Field columns into a number field, do calculations within a row or column. Extra merge tags are available with total counts of columns.
Field to Entries
Create entries based on Checkboxes & Multi Select choices & List Field rows.
Read tutorials about
Gravity Flow Form Submission Step Tutorial
The Gravity Flow Form Submission step pauses a workflow until another form is submitted, then continues once the handoff is complete. This guide shows how to connect two forms, prefill fields with mapping, send the correct secure link, and troubleshoot the most common “workflow didn’t move” issues.
Gravity Flow Delete an Entry Step Tutorial
The Gravity Flow Delete Entry step lets you trash or permanently remove Gravity Forms entries as part of a workflow. This guide covers the key settings, scheduling options (delay or date-based), and safe testing tips so you can follow retention and privacy rules with confidence.
Gravity Flow Update Fields Step Tutorial
The Gravity Flow Update Fields step pulls values from another entry and writes them into the current entry, so your workflow can route using “live” data. This tutorial covers source form selection, entry lookup with filters and sorting, field mapping, and quick testing.
Gravity Flow Update an Entry Step Tutorial
The Gravity Flow Update Entry step lets you update an existing Gravity Forms entry using a stored Entry ID and field mapping. This guide shows a simple parent-child setup where a Task Tracker workflow pushes status updates back into the original Request Form entry.