sanitize_filename()

function sanitize_filename(string $filename, bool $transliteration = true, int $max_length = 200): string

Description

Sanitizes a filename for safe storage and usage on the filesystem and web. This function removes or replaces special characters, handles international characters through transliteration, preserves file extensions, and prevents common security issues.

The function leverages url_title() internally to handle the heavy lifting of character conversion and normalization.

Security Note: This function throws an InvalidArgumentException if the filename contains null bytes, which can be used in directory traversal attacks.

Parameters

Parameter Type Description Default Required
$filename string The filename to sanitize (may include file extension). N/A Yes
$transliteration bool Whether to transliterate international characters to ASCII equivalents. Requires the 'intl' PHP extension. true No
$max_length int Maximum length for the base filename (excluding extension). Helps prevent filesystem issues. 200 No

Return Value

Type Description
string The sanitized filename with preserved and normalized extension.

What Gets Cleaned

The function handles multiple types of problematic characters:

  • International characters: Transliterates to ASCII (e.g., "Москва" → "moskva", "café" → "cafe")
  • Special characters: Removes or converts to dashes (e.g., "@#$%" → "")
  • Whitespace: Converts to dashes (e.g., "my file.jpg" → "my-file.jpg")
  • Multiple spaces/dashes: Collapses to single dashes
  • Parentheses and brackets: Removes them (e.g., "photo (1).jpg" → "photo-1.jpg")
  • File extensions: Preserves and normalizes to lowercase alphanumeric only

Example #1

Basic filename sanitization:

Example #2

The example above shows how to sanitize a user-uploaded filename before storing it on the server.

Example #3

The example above demonstrates handling international filenames with transliteration enabled.

Transliteration Examples: With transliteration enabled (requires PHP 'intl' extension):

  • "Москва.jpg" becomes "moskva.jpg"
  • "café résumé.pdf" becomes "cafe-resume.pdf"
  • "北京_beijing.jpg" becomes "bei-jing-beijing.jpg"
Without transliteration, international characters are simply removed, which may result in less readable filenames.

Example #4

The example above shows handling filenames that may conflict with existing files by adding unique identifiers.

Preventing Overwrites: Always check if a file exists before saving uploads. Add unique identifiers (timestamps, user IDs, or UUIDs) to prevent accidentally overwriting existing files. The sanitize_filename() function doesn't handle conflicts—that's your responsibility.

Example #5

The example above demonstrates using custom length limits for specific use cases.

Length Limits: Most filesystems support up to 255 bytes for filenames, but the default $max_length of 200 characters leaves room for extensions and gives you flexibility to add suffixes like "_thumb" or timestamps. Adjust this parameter based on your specific needs—shorter for thumbnails, longer for user-facing document names.