Created
July 12, 2016 08:10
-
-
Save sumanthkumarc/2de2e2cc06c648a9f52c121501a181df to your computer and use it in GitHub Desktop.
PHP function to sanitize the file name to be url safe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should also add < and > to the blocklist