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.
Read tutorials about
n8n vs Zapier: Which Automation Tool Actually Fits Your Work?
Trying to decide between n8n vs Zapier? This article breaks down pricing, hosting, integrations, AI features, and where self-hosted n8n on a cheap VPS can beat Zapier’s task-based plans. You’ll also see how Gravity Forms users can plug in the GravityWP n8n Connector and move key workflows off Zapier without rebuilding their forms.
Cloudflare Tunnel + n8n: Expose Local n8n for Webhook Testing (Dev Only)
This guide shows how to use Cloudflare Tunnel Quick Tunnels to put a local n8n instance on the internet for real webhook testing. It’s a dev-only recipe that lets tools like GravityWP’s n8n Connector call your local n8n over HTTPS, without renting a VPS or opening ports.
Self-Host n8n with Docker on a VPS
Learn how to self-host n8n with Docker on a VPS using a production-ready setup. This step-by-step guide walks you through configuring Docker Compose, Postgres, HTTPS, and WEBHOOK_URL so you can run reliable, secure n8n workflows on your own server.
One-Click n8n Hosting: How to Deploy n8n with Railway, Elestio & Coolify
Learn how one-click n8n hosting works on platforms like Railway, Elestio, and Coolify. This guide explains when to pick one-click hosting over n8n Cloud or a raw VPS, how templates are structured, and which env vars, security settings, and troubleshooting tips you can’t skip.