note 77383 deleted from function.mkdir by crell
| From: | crell@php.net | Date: | Wed, 29 Jun 2022 19:17:13 +0000 |
| Subject: | note 77383 deleted from function.mkdir by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218711@lists.php.net to get a copy of this message | ||
Note Submitter: Michal Nazarewicz, mina86 at mina86 dot com
----
On the other hand, splitting path on something else then a DIRECTORY_SEPARATOR may give unexpected
results when someone accualy wants a file name with backslash in it! Moreover, neither Alan's
nor pluto's code check for errors or return any value. Also, I don't like the
isset($folder[$i]) technique -- there is a count() function you know.
<?php
function recursive_mkdir($path, $mode = 0777) {
$dirs = explode(DIRECTORY_SEPARATOR , $path);
$count = count($dirs);
$path = '.';
for ($i = 0; $i < $count; ++$i) {
$path .= DIRECTORY_SEPARATOR . $dirs[$i];
if (!is_dir($path) && !mkdir($path, $mode)) {
return false;
}
}
return true;
}
?>
It will only fail if someone specifies a mode which does not allow owner to create new entries in
directory.