How to validate the file name of the Media File Uploads?

I want to validate the file name of the files to be uploaded from the Media Library.

CASE study: I want to restrict the file name containing ‘%’, ‘. .’, “https://wordpress.stackexchange.com/” etc..from uploading to the media library.

I can restrict the file types but not able to restrict as per the file names.

Suggest me any wordpress filter or any procedure to accomplish the solution.

1 Answer
1

The filter is sanitize_file_name. You get the $filename as parameter.

Sample code:

add_filter( 'sanitize_file_name', 'wpse_77892_filter_filename' );

function wpse_77892_filter_filename( $filename )
{
    return str_replace( '%', '-', $filename );
}

See my plugin Germanix URL for an extended example.

Leave a Comment