Creating a directory path not working with PHPs mkdir?
I have the following directory c:/files
and I'm trying to create a wrapper function which emulates mkdir()'s functionality except works on more then one folder e.g.
mkdir works fine on the following:
mkdir('c:/files/games', 0777);
But not on this:
mkdir('c:/files/games/say/yes', 0777);
Heres some rough code to further describe what I'm trying to achieve:
function mmkdir($path, $chmod = 0777) {
/* do so开发者_如何转开发me loop or something with mkdir()? here */
}
mmkdir('C:/tmp/something/something');
mmkdir('C:/tmp/go/something');
mmkdir('C:/tmp/yes');
mkdir works fine on the following:
mkdir('c:/files/games', 0777);
But not on this:
mkdir('c:/files/games/say/yes', 0777);
Wrong, mkdir()
has a third parameter
bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )
mkdir('c:/files/games/say/yes', 0777, true);
This makes your custom function obsolete. However, if you really want to create your own function (which in my opinion is not worth to think about, because it already exists built-in), it should look like
function recursive_mkdir ($path, $chmod = 0777) {
$parent = dirname($path);
if (!file_exists($parent)) recursive_mkdir($parent, $chmod);
mkdir($path, $chmod);
}
Call the mkdir()
function with the recursive parameter set to TRUE
. For example:
mkdir('c:/files/games/say/yes', 0777, TRUE);
精彩评论