[ad_1]
I know one can use a plugin to force WP to accept media files using existing filenames. Over the years, I’ve worked out my way of dealing with it–usually by changing the filename when updating images. But what exactly is the reason for WP’s behavior? Does WP leave behind trails even after files have been deleted? Does each upload change the database somehow?
[ad_2]
Specific logic is in the sanitize_file_name() function:
[https://github.com/WordPress/wordpress-develop/blob/7d606a30e2f475ee434abadff7271205766157d8/src/wp-includes/formatting.php#L2015-L2124](https://github.com/WordPress/wordpress-develop/blob/7d606a30e2f475ee434abadff7271205766157d8/src/wp-includes/formatting.php#L2015-L2124)
I may be missing a few things, but this is close to all of it described:
1. Removes special characters: ? [ ] / \ = < > : ; , “” ” & $ # * ( ) | ~ ` ! { } % + ’ « » ” “
2. Strips out UT8 PCRE characters
3. Verifies it’s an allowed extension:
4. Removes any newline/return characters
5. Replaces “%20” and “+” with dashes
6. Replace more than one dot in a row with a single one
7. Checks for multiple extensions, and if they exist postfix them with a trailing underscore if they are a 2-5 character long alpha string not in the allowed extension list
8. If a file doesn’t have an extension it checks the mime type and automatically adds one
9. If the name of the file was the extension only without any dots it’ll set the name to unnamed-file.{ext}