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;
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With