Created
April 25, 2011 15:52
-
-
Save vitorbrandao/940706 to your computer and use it in GitHub Desktop.
Sanitize PHP filenames for UNIX/Linux
This file contains 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
<?php | |
/** | |
* Helper holds a collection of static methods, useful for generic purposes | |
*/ | |
class Helper | |
{ | |
/** | |
* Returns a safe filename, for a given platform (OS), by replacing all | |
* dangerous characters with an underscore. | |
* | |
* @param string $dangerous_filename The source filename to be "sanitized" | |
* @param string $platform The target OS | |
* | |
* @return Boolean string A safe version of the input filename | |
*/ | |
public static function sanitizeFileName($dangerous_filename, $platform = 'Unix') | |
{ | |
if (in_array(strtolower($platform), array('unix', 'linux')) { | |
// our list of "dangerous characters", add/remove characters if necessary | |
$dangerous_characters = array(" ", '"', "'", "&", "/", "\\", "?", "#"); | |
} | |
else { | |
// no OS matched? return the original filename then... | |
return $dangerous_filename; | |
} | |
// every forbidden character is replace by an underscore | |
return str_replace($dangerous_characters, '_', $dangerous_filename); | |
} | |
} | |
// usage: | |
$safe_filename = Helper::sanitizeFileName('#my unsaf&/file\name?"'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As tqg5 said, you are missing a closing ")" on line 19