1

i have create hierarchically folder like folder under sub folder if exist samename folder than skip create folder and enter those directory and create nextone folder foldername taken from array

example

$folderarray = array('0'=>'8','1'=>'8','6'=>'9');

here array value 8 ,8 ,9 is folder name and i have try to create first folder name 8 than second create folder name 8 under folder 8 than create folder name 9 under folder name 8 structure like 8 under => 8 under => 9

how to do?

i have try using foreach like this but not create properly

foreach ($folderarray as $path){
   $source = "D:/xampp/htdocs/xyz/img/";
    $chkpath = $source.$path;

    if (!file_exists($chkpath)) {

        mkdir($chkpath, 0777, true);
        $source = $chkpath;


    }
    else{
        $source = $chkpath;
        continue;
    }
}
6
  • not create properly? maybe the parent folder has permission that blocks you from creating the folder? Commented Jul 17, 2019 at 6:32
  • folder created but not the proper way like folder under folder Commented Jul 17, 2019 at 6:35
  • So reading from the code, it seems like its creating folder /8 then inside folder /8, there is another folder /8 and then inside there is folder /9 right? Commented Jul 17, 2019 at 6:37
  • yes , i have create folder 8 than under create 8 than create 9 under 8 Commented Jul 17, 2019 at 6:40
  • now create all folder under $source path and this is bugg Commented Jul 17, 2019 at 6:41

2 Answers 2

1

you have mistake creating path after create someone folder you have forgot / in new path

try to like this

$folderarray = array('0'=>'8','1'=>'9','6'=>'1');

$source = "D:/xampp/htdocs/xyz/img/";

foreach ($folderarray as $path){

    $chkpath = $source.$path;
    if (!file_exists($chkpath)) {
        mkdir($chkpath, 0777, true);
        $source = $chkpath.'/';


    }
    else{
        $source = $chkpath.'/';

    }


}
Sign up to request clarification or add additional context in comments.

Comments

0

You might want to do:

$source = "D:/xampp/htdocs/xyz/img";
foreach ($folderarray as $path){
    $chkpath = $source . "/" . $path;

    if (!file_exists($chkpath)) {
        mkdir($chkpath, 0777, true);
        $source = $chkpath;
    } else {
        $source = $chkpath;
        continue;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.