Generate a Safe Filename from string
Login or Register to Bookmark this snippet
This function strips all bad characters from a filename, that has perhaps been uploaded from a windows environment but has characters not allowed in a unix filesystem
<?php
function GenerateSafeFileName($filename) {
$filename = str_replace("?","",$filename);
return $filename;
}
?>
Added by JC on 12th November, 2007
-
21st May, 2010 6:49 am Ashley Sheridan
-
What is wrong with using array arguments for str_replace which could reduce that lot by 8 lines.
Also, the second-to-last replace of the backslash is breaking the function, it should be escaped as a double backslash.
Also, not sure what Unix filesystems you've been using, but you can have characters such as # and ? in the filename. It's not recommended for web-based filenames because those characters have special meaning in a URL, but it does mean that your description is a little off.
You must be registered and logged in to post a comment.
Login here to post a comment