I was fiddling with my code, trying to write some code that will take data(including pictures) uploaded from a form via POST which will then create a directory complete with its associated subdirectories to store the image.

While writing the code, i kept getting the error

```
Warning: mkdir(): No such file or directory in C:\Users\Admin\Desktop\UniServer\www\AddItem.php on line 94

```

HOWEVER, when i set mkdir's resursion to true, mkdir suddenly works and the directory is created without any problems.

My Code:

```
if(isset($_FILES['upload']['tmp_name']))
 {
 $numfile=count($_FILES['upload']['tmp_name']);
{
    for($i=0;$i<$numfile;$i++)
    {
        if(is_uploaded_file($_FILES['upload']['tmp_name'][$i]))
        {
            //Conditionals for uploaded file
            $foldername=$_SESSION['UserId'];
            $cat=$_POST['category'];
            $sub=$_POST['subcat'];
            $itemname=$_POST['itemname'];
            $allowed_filetypes=array('.jpg','.gif','.bmp','.png');
            $max_filesize = 2097152; // Maximum filesize in BYTES (currently 2.0MB).
            $upload_path = 'C:\Users\Admin\Desktop\UniServer\www\images\\'.$foldername.'\\'.$cat.'\\'.$sub.'\\'.$itemname.'\\'; // The place the files will be uploaded to.
            //Checks if Folder for User exists
            //If not, A folder for the user is created to store the user's images
            if(!file_exists($upload_path))
            {
                $upload_path=mkdir($upload_path,0644,true);<-- This is the line
            }

            $filename = $_FILES['upload']['name'][$i]; // Get the name of the file (including file extension).
            $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

            // Check if the filetype is allowed, if not DIE and inform the user.
            if(!in_array($ext,$allowed_filetypes))
            {
                die('The file you attempted to upload is not allowed.');
            }

            // Now check the filesize, if it is too large then DIE and inform the user.
            if(filesize($_FILES['upload']['tmp_name'][$i]) > $max_filesize)
            {
                die('The file you attempted to upload is too large.');
            }

            // Check if we can upload to the specified path, if not DIE and inform the user.
            if(!is_writable($upload_path))
            {
                $errormsg="Image Upload Failed.";
            }

            if(!move_uploaded_file($_FILES['upload']['tmp_name'][$i],"$upload_path" . $filename))
            {
                $errormsg= 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
            }

        }
    }
}

 }
   else{echo"Upload failed";}

```

While my code is working now that i've set recursion to true, i still DON't understand exactly WHY it is working, so i would really appreciate it if someone could explain why exactly my code is working.

The closest i've come is with [Why mkdir fails with recursive option set true?](https://stackoverflow.com/questions/13599046/why-mkdir-fails-with-recursive-option-set-true)

Though i couldnt understand any of what was said in the link.

Thanks!