note 42152 deleted from function.mkdir by tomsommer
| From: | tomsommer@php.net | Date: | Wed, 05 May 2004 15:21:44 +0000 |
| Subject: | note 42152 deleted from function.mkdir by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-69237@lists.php.net to get a copy of this message | ||
Note Submitter: Linuxworld
----
Let's have some remarks, the second regular expression given in the example below crashed when
you got - in names. I fixed it. Moreover, I have considered here that when the directory is alreaded
created, this is considered as success and nothing need to be done. I don't like either to let
structures without delimiters, taking account explains are your ONLY FRIENDS when something WRONG
HAPPENS.Notice this mkdir_p will not work properly with windows pathnames (containing \ as usual).
function mkdir_p($target){
//------------------------
if (file_exists($target) && !is_dir($target)) {
//echo "Failure, the directoy name conflicts with an existing filename ";
return 0;
}
if (substr($target, 0, 1) != '/') {
$target = "/".$target; // prepend a slash
}
if (ereg('\.\.', $target) || ereg("[a-zA-Z0-9]+[\.\-\/]*", $target)) {
//echo "illegal dirname".htmlspecialchars($target);
return 0;
}
if (is_dir($target) || @mkdir($target)) { // if new dir created or directory already created
return 1; // we are successful
} else {
//echo "mkdir unsuccesfull , trying further with recursivity";
}
if (mkdir_p(substr($target, 0, (strrpos($target, '/')))) == 1) {
return (mkdir_p($target));
} else {
//echo "mkdir_p failure, please check you rights for the directory
".htmlspecialchars($target) ;
return 0;
}
}