PHP create nested directories

you can try using file_exists to check if a folder exists or not and is_dir to check if it is a folder or not.

 if(file_exists($dir) && is_dir($dir))

And to create a directory you can use the mkdir function

Then the rest of your question is just manipulating this to suit the requirements


recursive Allows the creation of nested directories specified in the pathname. but did not work for me!! for that here is what i came up with!! and it work very perfect!!

$upPath = "../uploads/RS/2014/BOI/002";   // full path 
$tags = explode('/' ,$upPath);            // explode the full path
$mkDir = "";

    foreach($tags as $folder) {          
        $mkDir = $mkDir . $folder ."/";   // make one directory join one other for the nest directory to make
        echo '"'.$mkDir.'"<br/>';         // this will show the directory created each time
        if(!is_dir($mkDir)) {             // check if directory exist or not
          mkdir($mkDir, 0777);            // if not exist then make the directory
        }
    }

Use the third parameter of mkdir():

recursive Allows the creation of nested directories specified in the pathname. Defaults to FALSE.

$path = '/path/to/folder/with/subdirectory';
mkdir($path, 0777, true);