aWebDeveloper
aWebDeveloper

Reputation: 38392

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)))
            {
                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;
    }
 }

Upvotes: 5

Views: 18076

Answers (3)

Raisen
Raisen

Reputation: 4495

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

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

Upvotes: 1

atx
atx

Reputation: 5079

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.

Upvotes: 1

sarnold
sarnold

Reputation: 104080

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...');
}

// ...
?>

Upvotes: 5

Related Questions