note 60825 added to function.mkdir
| From: | baldurien at club-internet dot fr | Date: | Tue, 17 Jan 2006 23:14:48 +0000 |
| Subject: | note 60825 added to function.mkdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-101992@lists.php.net to get a copy of this message | ||
For those wandering about the $recursive flag of mkdir in php5, this function will do what you want
(à la java) in php 4:
<?php
/**
* Create a new directory, and the whole path.
*
* If the parent directory does not exists, we will create it,
* etc.
*
* @param string the directory to create
* @param int the mode to apply on the directory
* @return bool return true on success, false else
*/
function mkdirs($dir, $mode = FS_RIGHTS_D) {
$stack = array(basename($dir));
$path = null;
while ( ($d = dirname($dir) ) ) {
if ( !is_dir($d) ) {
$stack[] = basename($d);
$dir = $d;
} else {
$path = $d;
break;
}
}
if ( ( $path = realpath($path) ) === false )
return false;
$created = array();
for ( $n = count($stack) - 1; $n >= 0; $n-- ) {
$s = $path . '/'. $stack[$n];
if ( !mkdir($s, $mode) ) {
for ( $m = count($created) - 1; $m >= 0; $m-- )
rmdir($created[$m]);
return false;
}
$created[] = $s;
$path = $s;
}
return true;
}
?>
The main idea here is to use a stack to store the invalid dirname (using basename/dirname), and then
pop each item on the stack to create directory from a valid path (here: $path = realpath($path). If
we fails creating one directory, we must remove them in the reverse order of creation (stackes :)).
This works on Windows XP, and it should work on Linux (actually, I did not tested it).
Note that this code is a part of one of my classes (named FileSystem), and that you should use your
own basename/realpath/dirname/mkdir/rmdir function instead of php one (even if you use them behind).
This will allow you to separate the item of a FileSystem from what is it really (FTP? Local ?
Telnet? Teletubbies?)
----
Server IP: 217.174.203.10Probable Submitter: 212.194.97.50
----
Manual Page -- http://www.php.net/manual/en/function.mkdir.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60825
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60825&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60825&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60825&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60825&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60825&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60825&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60825&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60825&report=yes
Search -- http://master.php.net/manage/user-notes.php