Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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;
    }
 }
like image 701
aWebDeveloper Avatar asked Mar 08 '11 06:03

aWebDeveloper


2 Answers

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

// ...
?>
like image 129
sarnold Avatar answered Oct 09 '22 03:10

sarnold


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.

like image 29
atx Avatar answered Oct 09 '22 04:10

atx