note 38398 added to function.mkdir
| From: | info at phplabs dot com | Date: | Fri, 19 Dec 2003 09:17:54 +0000 |
| Subject: | note 38398 added to function.mkdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-62219@lists.php.net to get a copy of this message | ||
The following code should simulate the unix command
mkdir -p to a fair degree. If no
part of the path is writable, the recursion will complete gracefully without the directory being
created. When running through Apache, it will require some directory along the way to be chmod 777
or chowned to "nobody."
Example: suppose you have the directory /home/you/public_html and you want to create the tree
/home/you/public_html/foo/bar/baz/some/long/path. Your host doesn't allow exec() but you
don't want to manually issue a mkdir() for each step. You can pass the target directory to the
following function.
Returns 0 on failure, 1 on success. Note that I chose to report failure if the specified path is
already there, you may want to see that as a success.
<?
function mkdir_p($target){
if(file_exists($target) || is_dir($target))
return 0;
if(substr($target, 0, 1) != '/')
$target = "/$target";
if(eregi('\.\.', $target) || eregi("[^a-zA-Z0-9\._\-\/]", $target))
return 0;
if(mkdir($target)){
return 1;
}
if(mkdir_p(substr($target, 0, (strrpos($target, '/')))) == 1){
if(mkdir_p($target) == 1)
return 1;
else
return 0;
}
else{
return 0;
}
}
?>
Enjoy
Shaun
----
Manual Page -- http://www.php.net/manual/en/function.mkdir.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+38398
Delete -- http://master.php.net/manage/user-notes.php?action=delete+38398&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+38398&report=yes
Search -- http://master.php.net/manage/user-notes.php