Search code examples
phpfunctionmkdirfine-uploader

Creating and chmoding multiple directories with mkdir


I'm using mkdir function to create and chmod directories $dirX $dirY.

The following code block creates $dirY only and upload the desired file there. What's going wrong here? Why the other directory isn't being created along with the uploaded file?

$dirA = 'mydir1/';     
$dirB = '../mydir2/';   
$directory = array('$dirA','$dirB');
foreach ($directory as $dir); 


if (!is_dir($dir)){ 
mkdir($dir, 0777)
};


for($f=0; $f<count($_FILES['newsimage_upload']['name']); $f++) {
    $nume_f = $_FILES['newsimage_upload']['name'][$f];

   $thefile = $dir . '/'. $nume_f; //It doesn't set the directories of array's strings

 if (!move_uploaded_file ($_FILES['newsimage_upload']['tmp_name'][$f], $thefile)) {
              $uploadresult[$f] = 'The file '. $nume_f. 'could not be copied, try again';
}

Solution

  • Your code should be something similar to:

    $dirA = 'mydir1/';     
    $dirB = '../mydir2/';   
    $directory = array('$dirA','$dirB');
    foreach ($directory as $dir){
    
    if (!is_dir($dir)) mkdir($dir, 0777);
    
    
    for($f=0; $f<count($_FILES['newsimage_upload']['name']); $f++) {
        $nume_f = $_FILES['newsimage_upload']['name'][$f];
    
       $thefile = $dir . '/'. $nume_f; //It doesn't set the directories of array's strings
    
     if (!move_uploaded_file ($_FILES['newsimage_upload']['tmp_name'][$f], $thefile)) {
                  $uploadresult[$f] = 'The file '. $nume_f. 'could not be copied, try again';
    }
    //some more code
    } //closing the for
    //some more code
    } //closing the foreach
    

    Note that in your original code example there is a missing closing curly brace for the for loop, so I assume you closed it in your original code before the final foreach closing brace.