Simple way to clean up filenames in PHP

Posted by on November 1, 2011 in PHP, Programming | 0 comments

Simple way to clean up filenames in PHP

If you want to make sure no silly characters are part of filenames, then you’ll want to sanitize any filenames of files you store on your server. This will make things a whole lot easier in the long run. Certain functions like file_exists(), will return false if the filename contains a %20 instead of a space, even if the file does exist and you can pull it up in your browser. Sanitation will also help if you port your system to a different operating system if you ever move.

You can expand this, but this will remove everything except numbers, letters and (.) periods.

preg_replace(“/[^a-z0-9\.]/”, “”, strtolower($str));
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment

Your email address will not be published. Required fields are marked *