1

Sorry to bother you with this. I'm running mkdir to replicate directories that I have stored in a DB.

If I display the data on a php page the directories look like this:

element1/Content/EPAC/PROD
element1/Content/EPAC/TEST
element1/Content/EPAC_SG/PROD
element1/Content/EU/PROD
element1/Content/EU/TEST

The above is a subset of the data. What is happening with the above subset when I loop through it, it creates the directory element1/Content/EPAC/PROD but ignores element1/Content/EPAC/TEST and element1/Content/EPAC_SG/PROD, Then it creates element1/Content/EU/PROD but ignores element1/Content/EU/TEST etc and continues through the loop like that. The code I'm using is:

foreach($NSRarray as $value)
{
    mkdir("ftpfolders/$value", 0700, true);
}

*the $value variable above is the 'element1/Content/EPAC/PROD' record taken from the DB.

Any ideas? Thanks in advance, Ste

2
  • whats the question and whats the error? Commented Jun 18, 2013 at 10:47
  • Did you ever solved the problem ? :) If so please accept the corresponding answer. Commented Apr 7, 2016 at 12:51

2 Answers 2

1

I would split this into generating directories one at a time.

Transform your array into sth. like

$dics=array(
'element1' => array(
   'Content' => array(
      'EPAC' => array('PROD', 'TEST'),
      'EPAC_SG' => array('PROD')
      'EU' => array('PROD', 'TEST')
     )
   )
);

Then loop over it, starting with array_keys($dics) and create the directory if not existing. Continue with array_keys($dics['element1']) and then repeat it until your reach the inner childs.

Hope this helps.

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

1 Comment

Hi, Thank you so much for answering. I've just actually managed to fix it. Turns out the mkdir after a certain level was adding a space. I did a string replace to remove the space and it created my directories as I would hope. Thank you so much for the intelligent answer though, I'm sure I'll use your idea when I'm stuck again.
1

use this code, this will gives you proper folder structure as per your requirement

<?php
$NSRarray = array('element1/Content/EPAC/PROD', 'element1/Content/EPAC/TEST', 'element1/Content/EPAC_SG/PROD','element1/Content/EU/PROD','element1/Content/EU/TEST');

foreach($NSRarray as $value)
{
    $getFolders = explode('/' , $value);
    $mainFoldername = "ftpfolders";
    $countfolder = 0;
    $countfolder = count($getFolders);
    $tempName = "";
    $i = 0;
    for($i == 0; $i < $countfolder; $i++){
        $tempName .= $getFolders[$i];
         if (!file_exists("$mainFoldername/$tempName")) {
            mkdir("$mainFoldername/$tempName", 0700, true);
        } 
        $tempName .= '/';
    }  
}

?>

1 Comment

Hi, thank you very much for the reply. I had just managed to fix it already as per the comment above, but I did add your !file_exists to the code, which certainly helped tidy it up. Thanks so much for the reply, hope I can help you out sometime

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.