/**
* @filter sanitize_file_name
*/
public function sanitize(string $name): string
{
$name = strtolower(remove_accents($name));
$name = preg_replace('/[^a-z0-9-\. ]/', '', $name);
return $name;
}
Purpose
WordPress by default uses sanitize_file_name
function during upload to avoid problems with filesystem. But sometimes we need something more.
Once, I've been facing problems with different server infrastructures, because one environment was not able to handle special chars like å
in the filenames and the images were not shown. Of course, we can probably fix this by changing environment configuration, but I'm not specialized in managing servers so I just don't want to touch this if not needed.
So I've just used sanitize_file_name
filter for implementing simple agorithm to do something more. It uses remove_accents
function, makes name lowercase and removes characters other than the ones set in regular expression. It matches mostly all my requirements and fits to most of the projects.
Plugins
You're free to implement your own sanitization methods so please do not use plugins if you need something super simple! You can reduce the following plugins from your infrastructure.