title: Create folder and upload images in php/codeigniter
body: <p>I have a simple image upload form on my website where users can upload multiple images at once. I want to have my images organized in folder based on the month and year in the following format: MONTH-YEAR, so each time new upload starts I first check whether that folder exists or not, and create one if it doesn't exists.</p>
  
  <p>The problem is that if the folder doesn't exists for the current month and I try to upload an image, the folder, representing this current month, is created properly, but then no image is uploaded. But if the folder already exists all the images can be uploaded without any problem. Here is my code:</p>
  
  <pre><code>    $folderName = date('m-y');
      $pathToUpload = './uploads/photos/' . $folderName;
      if ( ! file_exists($pathToUpload) )
      {
          $create = mkdir($pathToUpload, 0777);
          $createThumbsFolder = mkdir($pathToUpload . '/thumbs', 0777);
          if ( ! $create || ! $createThumbsFolder)
          return;
      }
  
      $imgName= uniqid('', TRUE);
      $config['upload_path'] = $pathToUpload;
      $config['allowed_types'] = 'gif|jpg|png';
      $config['max_size'] = '9999';
      $config['file_name'] = $imgName . '.jpg';
  
      $this-&gt;upload-&gt;initialize($config);
      $upload = $this-&gt;upload-&gt;do_upload("Filedata");
  </code></pre>
  
  <p>Any ideas why the upload doesn't work for the very first time?</p>