☰

TutorialHow to Sanitize Gravity Forms Uploaded Filenames

Quick answer: WordPress already cleans many filenames during upload processing. In our Gravity Forms 3.1.3 staging test, common accents and spaces changed without custom code. Duplicate names received a numeric suffix, and some symbols were removed. You only need a custom rule when your workflow needs a stricter format, such as lowercase ASCII filenames.

This guide shows what happened in our real Gravity Forms File Upload field tests. It then adds a stricter WordPress filename filter and compares the results. Your results can vary with WordPress, Gravity Forms, other plugins, and server settings.

Need Gravity Forms? Get Gravity Forms and use its File Upload field for the examples in this tutorial.

Key Takeaways

  • WordPress already provides core filename sanitization, including accent handling, whitespace cleanup, and special-character rules.
  • A custom sanitize_file_name filter can enforce lowercase ASCII filenames, but the filter is global and is not limited to Gravity Forms.
  • Our strict rule made filenames more predictable, but it also replaced a fully Japanese basename with uploaded-file.
  • Filename cleanup is not the same as file validation, malware scanning, access control, or secure storage.
  • Test the rule on staging before production, including uploads, exports, and any automation that depends on filenames.

How Does WordPress Sanitize Uploaded Filenames?

Direct answer: WordPress has a core sanitize_file_name() function. It calls remove_accents() first, also removes a defined set of special characters and normalizes whitespace to dashes. It trims certain punctuation at the start and end and has special handling for filenames with multiple extensions.

After the core function finishes, WordPress exposes the sanitize_file_name filter. That is the filter used later in this tutorial. In our Gravity Forms staging test, the filter changed the stored upload filename as expected.

Gravity Forms also has its own File Upload settings for file size and allowed extensions. Those settings solve a different problem. They control what can be uploaded, while filename sanitization controls how the name is cleaned.

What Gravity Forms Did With Uploaded Filenames by Default

Test setup: Gravity Forms 3.1.3, one single File Upload field, no custom filename filter, tested on staging on October 6, 2026.

We uploaded seven filename patterns to see what was saved in the Gravity Forms entry. These are observed test results, not assumptions.

Original filenameSaved filenameWhat we observed
Résumé été.txtResume-ete.txtCommon Latin accents removed; spaces became hyphens.
My File With Spaces.txtMy-File-With-Spaces.txtSpaces became hyphens.
price-€-£-$-100.txtprice-£-100.txtSymbols did not all behave the same in this test.
report.final.version.txtreport.final_.version.txtThe multi-dot name was changed.
UPPERCASE.TXTUPPERCASE.TXTUppercase name and extension stayed uppercase.
duplicate.txt, uploaded twiceduplicate.txt, then duplicate1.txtThe second file received a numeric suffix.
日本語ファイル.txt日本語ファイル.txtThe Japanese basename was preserved.
Gravity Forms upload test showing Résumé été.txt saved as Resume-ete.txt by WordPress filename sanitization
Original GravityWP test: the accented filename “Résumé été.txt” was saved as “Resume-ete.txt” without custom filename code.

The main lesson is simple: do not add custom code just to remove common accents or spaces until you test the native result first.

When Do You Need Stricter Filename Sanitization?

Direct answer: Use a stricter rule only when the default filename format does not meet a real workflow requirement.

A stricter filename can help when another system expects simple lowercase ASCII names. It can also help when scripts rely on a predictable pattern or a team wants one format across uploads. This can matter when files move through Gravity Forms integrations, APIs, storage systems, or automation tools.

It can also help on public forms that collect files such as logos or documents. For example, our public submission business directory tutorial uses a Gravity Forms File Upload field for business logos.

Do not use ASCII-only cleanup by default on a multilingual site. A strict rule can remove meaningful text from filenames written in Japanese, Arabic, Chinese, Greek, and other scripts.

Add a Strict Filename Policy in WordPress

Scope warning: The sanitize_file_name filter is a WordPress-wide filter. It can affect filenames processed outside this one Gravity Forms field. Test media uploads, Gravity Forms uploads, and export workflows on staging before you use it in production.

Gravity Forms has documented cases where a poorly designed sanitize_file_name filter can interfere with CSV exports. See the official Gravity Forms export troubleshooting guide. Our snippet does not use the problematic random-name pattern described there, but the global scope is still a reason to test carefully.

Add the code with a code snippets plugin or a small site-specific plugin. This keeps the rule separate from your theme.

add_filter( 'sanitize_file_name', function ( $filename, $filename_raw ) {
    $extension = pathinfo( $filename, PATHINFO_EXTENSION );
    $basename  = pathinfo( $filename, PATHINFO_FILENAME );
    // Lowercase the filename.
    $basename = strtolower( $basename );
    // Keep only ASCII letters and numbers.
    // Replace every other group of characters with one hyphen.
    $basename = preg_replace( '/[^a-z0-9]+/', '-', $basename );
    $basename = trim( $basename, '-' );
    // Avoid an empty basename for filenames made only from
    // characters outside this strict ASCII policy.
    if ( $basename === '' ) {
        $basename = 'uploaded-file';
    }
    $extension = strtolower( $extension );
    return $extension !== ''
        ? $basename . '.' . $extension
        : $basename;
}, 20, 2 );

This rule works on the filename WordPress has already processed. It lowercases the basename and extension. It keeps only the letters a-z and numbers 0-9 in the basename. Other character groups become one hyphen. It uses uploaded-file if the basename becomes empty.

The second callback argument, $filename_raw, is the original filename supplied to the WordPress hook. This snippet accepts it to match the hook signature, but it does not need the raw value for this strict policy.

What Changed With the Strict Filename Policy?

We kept the same Gravity Forms test form and enabled the strict filter. The table below compares the native result with the strict result.

Original filenameNative resultStrict result
UPPERCASE.TXTUPPERCASE.TXTuppercase.txt
Résumé été.txtResume-ete.txtresume-ete.txt
My File With Spaces.txtMy-File-With-Spaces.txtmy-file-with-spaces.txt
price-€-£-$-100.txtprice-£-100.txtprice-e-100.txt
report.final.version.txtreport.final_.version.txtreport-final-version.txt
repeat-test.txt, uploaded twiceNot used in native setrepeat-test.txt, then repeat-test1.txt
日本語ファイル.txt日本語ファイル.txtuploaded-file.txt
Gravity Forms upload test showing report.final.version.txt changed to report-final-version.txt with a strict filename filter
Original GravityWP test: the strict filter changed “report.final.version.txt” to “report-final-version.txt”.

The strict policy made the output more predictable. It also changed more than the native flow. That extra cleanup is useful only if your workflow actually needs it.

Does WordPress Remove Accents From Filenames?

Yes, for many common accented characters. WordPress calls remove_accents() inside its filename sanitization function. The exact replacements are defined by WordPress and some depend on locale.

In our native Gravity Forms test, Résumé été.txt became Resume-ete.txt. The strict filter then lowercased that result to resume-ete.txt. This means the custom regex was not needed just to remove those accents.

How Does WordPress Handle Spaces and Special Characters in Filenames?

Spaces are normally normalized, but special characters do not all have one universal outcome. The official sanitize_file_name() documentation lists characters that WordPress removes and explains how whitespace is converted to dashes.

Our test showed why it is better to verify than to guess. My File With Spaces.txt became My-File-With-Spaces.txt. The symbol test price-€-£-$-100.txt became price-£-100.txt in the native Gravity Forms test and price-e-100.txt with the strict filter.

WordPress also has special logic for multiple extensions. Intermediate extension-like parts can receive an underscore when they are not in the allowed extension list. That helps explain why report.final.version.txt became report.final_.version.txt in our native test.

Can You Make Uploaded Filenames Lowercase in WordPress?

Yes. The strict filter above lowercases both the basename and the extension. In our native test, UPPERCASE.TXT stayed uppercase. With the filter active, it became uppercase.txt.

Lowercase names can make automation rules and naming conventions easier to manage. They are not required for every WordPress or Gravity Forms site.

Should You Remove Non-Latin Characters From Filenames?

Only if your destination requires ASCII-only filenames. The strict regex in this tutorial allows only a-z and 0-9 in the basename. That means a filename written entirely in another script can lose its original text.

Our Japanese test showed the trade-off clearly. Native handling preserved 日本語ファイル.txt. The strict policy converted it to uploaded-file.txt because the basename had no ASCII letters or numbers left after the regex.

Gravity Forms upload test showing Japanese filename converted to uploaded-file.txt by an ASCII-only filename rule
Original GravityWP test: the ASCII-only rule changed the Japanese filename “日本語ファイル.txt” to the fallback “uploaded-file.txt”.

For multilingual sites, native Unicode filenames may be the better choice. Do not call the ASCII-only result “cleaner” unless compatibility with another system is the actual requirement.

Filename Sanitization Is Not File Upload Security

A clean filename does not prove that a file is safe. Filename sanitization only changes the name. It does not replace file-type checks, allowed-extension rules, malware controls, access control, or storage policy.

Gravity Forms can restrict allowed extensions and maximum file size in the File Upload field settings. Its documentation also explains that uploaded file URLs are obscured but are not access-controlled by default. If uploaded documents are sensitive, review the official File Upload Security guide before launch.

How to Remove or Roll Back the Custom Filename Policy

To roll back, disable the snippet or remove the filter from your site-specific plugin. Future filenames will then follow WordPress core and any other active filters.

Files that were already uploaded are not automatically renamed back. If an existing file must be renamed, treat that as a separate migration task. Test any saved links or integrations that depend on the old name.

Using Gravity Forms With Sanitized Uploaded Files

In our working strict-filter tests, Gravity Forms still saved a file link in the entry. The duplicate test also kept working: the first repeat-test.txt stayed as-is and the second became repeat-test1.txt. We did not add custom duplicate logic.

If you include file-upload links in reports, exports, or other downstream processes, retest those workflows after any global filename rule. Our Gravity Forms Entries in Excel tutorial is one example of a workflow where file-upload links can be part of exported entry data.

Want to build the form used in this type of workflow? Get Gravity Forms (affiliate link).

Final Takeaway

WordPress already does more filename cleanup than many sites need. In our Gravity Forms tests, common accents and spaces were handled without custom code. A stricter sanitize_file_name rule is useful when a real system requires lowercase ASCII names. It has a cost: non-Latin filenames can lose their meaning.

Test the native behavior first. Add stricter rules only when the workflow needs them. Then test the full path from upload to entry, export, automation, and storage.

For more practical guides, browse the GravityWP tutorial library.

Frequently Asked Questions

Does Gravity Forms sanitize uploaded filenames?

In our staging test, a WordPress sanitize_file_name filter changed filenames submitted through a Gravity Forms File Upload field. This confirms the filter is part of the upload path we tested.

Does WordPress remove special characters from uploaded filenames?

WordPress removes a defined set of special characters and normalizes whitespace, but different characters can produce different results. Test the filenames that matter to your workflow.

Does WordPress remove accents from filenames?

WordPress uses remove_accents() during sanitize_file_name(). In our test, “Résumé été.txt” became “Resume-ete.txt” without custom filename code.

Can I force WordPress upload filenames to lowercase?

Yes. A sanitize_file_name filter can lowercase the basename and extension. The snippet in this tutorial did that in our Gravity Forms test.

Does sanitizing a filename make the uploaded file secure?

No. Filename cleanup is separate from file-type validation, malware controls, access control, and secure storage.

Proudly powered by WordPress