note 33231 deleted from function.mkdir by didou
| From: | didou@php.net | Date: | Fri, 03 Oct 2003 11:41:50 +0000 |
| Subject: | note 33231 deleted from function.mkdir by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-57714@lists.php.net to get a copy of this message | ||
Note Submitter: malone@N0SPAM.vwmark.com
----
^^ 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.