note 33231 added to function.mkdir
| From: | malone at N0SPAM dot vwmark dot com | Date: | Thu, 19 Jun 2003 18:15:06 +0000 |
| Subject: | note 33231 added to function.mkdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-50672@lists.php.net to get a copy of this message | ||
^^ I'd also like to know how that mkdirs() function works.
"KilerCris at Mail dot com"'s mkpath() function:
function mkpath($path,$mode = 0700) {
$dirs = explode("\\",realpath($path));
$path = $dirs[0];
for($i = 1;$i < count($dirs);$i++) {
$path .= "/".$dirs[$i];
if(!is_dir($path))
mkdir($path,$mode);
}
}
^ The use of explode('\\'..) causes problems with syntax colouring in my PHP editor - the
result is much like placing a /* without the closing */, so I used preg_split instead and simplified
some other things:
function mkpath($path, $mode = 0700)
{
$dirs = preg_split('/[\/]+/', realpath(dirname($path)));
while ( list($key, $val) = each($dirs) )
{
$path .= $path ? "/" . $val : $val;
if( !is_dir($path) && !mkdir($path, $mode) )
return false;
}
return true;
}
Note that I used the dirname() function for $dirs because I always pass paths including filenames.
Remove dirname() if your paths contain only directory names. Thanks to KilerCris for writing a great
function for me to base on.
----
Manual Page -- http://www.php.net/manual/en/function.mkdir.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33231
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33231&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33231&report=yes