note 72052 deleted from ref.filesystem by felipe
| From: | felipe@php.net | Date: | Mon, 10 Dec 2007 18:01:41 +0000 |
| Subject: | note 72052 deleted from ref.filesystem by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-133240@lists.php.net to get a copy of this message | ||
Note Submitter: designatevoid at gmail dot com
----
Here's a function that checks if a string filename, absolute or relative, is valid or not.
<?php
function validFilename($file)
{
if( checkCurrentOS( 'Win' )) {
// if has invalid filename, return false
if ( preg_match( '/PRN|CON|AUX|CLOCK\$|NUL|COM\d|LPT\d/i', $file)) {
return false;
}
$parts = explode( "\\", $file);
// if a drive letter, approve (don't check it later)
if (preg_match( '/[A-Za-z]:/', $parts[0] )) {
$parts[0] = null;
}
// checks for invalid printable charactors or control charactors
foreach ($parts as $part) {
if( preg_match( '|[\^\|\?\*<\":>/\a\b\e\f\n\r\t]|', $part )) {
return false;
}
}
return true;
} else { // unix always passes
return true;
}
}
function checkCurrentOS($operatingSystem)
{
$currentOS = substr( php_uname(), 0, 7 ) == "Windows" ? "Win" :
"_Nix";
if ( strcmp( $operatingSystem, $currentOS ) == 0 ) {
return true;
} else {
return false;
}
}
?>