Remove Special Characters From Uploaded Files

2019-09-05 13:58发布

问题:

I am facing issues regarding the filenames that were accented and found a function to sanitize the filenames, but I am not sure of if this will affect the filenames in database as well or will just rename the files?

Here is the code:

add_filter('sanitize_file_name', 'sa_sanitize_spanish_chars', 10);
function sa_sanitize_spanish_chars ($filename) {
return remove_accents( $filename );
}

回答1:

Try below code in function file.

function sa_sanitize_spanish_chars($filename) {
$ext = end(explode('.',$filename));
$sanitized = preg_replace('/[^a-zA-Z0-9-_.]/','', substr($filename, 0, -(strlen($ext)+1)));
$sanitized = str_replace('.','-', $sanitized);
return strtolower($sanitized.'.'.$ext);
}

add_filter('sanitize_file_name', 'sa_sanitize_spanish_chars', 10);