Skip to content

Instantly share code, notes, and snippets.

@sumanthkumarc
Created July 12, 2016 08:10
Show Gist options
  • Save sumanthkumarc/2de2e2cc06c648a9f52c121501a181df to your computer and use it in GitHub Desktop.
Save sumanthkumarc/2de2e2cc06c648a9f52c121501a181df to your computer and use it in GitHub Desktop.
PHP function to sanitize the file name to be url safe
/*
* Function to sanitize the file name for url and file name safe.
*
* @param string $filename
* The unsafe filename is taken as input
*
* @return string
* The safe file name is returned.
*/
function filename_sanitizer($unsafeFilename){
// our list of "unsafe characters", add/remove characters if necessary
$dangerousCharacters = array(" ", '"', "'", "&", "/", "\\", "?", "#");
// every forbidden character is replaced by an underscore
$safe_filename = str_replace($dangerousCharacters, '_', $unsafeFilename);
return $safe_filename;
}
@LoganTann
Copy link

You should also add < and > to the blocklist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment