Remove Special Characters From Uploaded Files

2019-09-05 14:04发布

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条回答
太酷不给撩
2楼-- · 2019-09-05 14:32

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);
查看更多
登录 后发表回答