# Clean Up File Names on Upload

> Converts uploaded file names to lowercase, removes accents and replaces spaces and symbols with dashes, for tidy and portable URLs.

- Type: PHP
- Runs: Run everywhere
- Category: [Media](https://wpide.com/library/category/media/)
- Tags: media, uploads, file-names
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/clean-upload-file-names/

## Code

```php
add_filter( 'sanitize_file_name', function ( $filename ) {
    $info      = pathinfo( $filename );
    $extension = isset( $info['extension'] ) && '' !== $info['extension'] ? '.' . strtolower( $info['extension'] ) : '';
    $name      = isset( $info['filename'] ) ? $info['filename'] : $filename;

    $name = remove_accents( $name );
    $name = strtolower( preg_replace( '/[^A-Za-z0-9]+/', '-', $name ) );
    $name = trim( $name, '-' );

    return ( '' === $name ? 'file' : $name ) . $extension;
} );
```

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Clean Up File Names on Upload" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
