开发者

Warning (2): mkdir() [function.mkdir]: No such file or directory

Hi i recently faced this problem but was able to fix it. Actually spelling mistake in path. I want to know how to handle these error properly. i.e my program should continue executing and should safely return a false if mkdir fails. This is my code

try
{
    foreach($folders as $folder)
    {
        $path  = $path.'/'.$folder;    
        if(!file_exists($path))
        {
            if(!(mkdir($path)))
            {
  开发者_JAVA百科              return false;
            }
        }
    }
    return true;
}
catch (Exception $e){
    return false;
}

I just want if mkdir is not able to create it. It should return a false and the execution should continue

EDIT: Here is updated code based on community feedback. But still no proper answer to my question

if(!file_exists($newfolder))
 {
    if(mkdir($newfolder,0755,true))
    {
                return true;
    }
 }


Are you looking for setting the recursive flag to true?

<?php
// Desired folder structure
$structure = './depth1/depth2/depth3/';

// To create the nested structure, the $recursive parameter 
// to mkdir() must be specified.

if (!mkdir($structure, 0, true)) {
    die('Failed to create folders...');
}

// ...
?>


The function appears to not be recursive. You will have to make the entire directory tree, down to your directory that you want to create. Read here. Like sarnold said, just set the recursive argument to true.


Take a look at this sample, it might be what you are looking for.

http://www.php.net/manual/en/function.mkdir.php#92844

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜