title: Save Image to the Designated Folder and Subfolder using PHP?
body: <p>I have a PHP that show the number of the image in the PHP. Now i want to upload the image based on the Image count to the different folders here is the PHP below to show the Image count and the other PHP that I want to use to upload the image.</p>
  
  <p>First PHP:</p>
  
  <pre><code>&lt;?php  
  include('includes/config.php');
   $upload = 'uploads/';
  
   session_start();
  $_SESSION['userid'];
  
      $sql = "SELECT * FROM tbl_job WHERE username = '$userid' ";
  
      $result = mysqli_query($conn,$sql);
          $rowcount=mysqli_num_rows($result);
      $job = array();
      $client = array();
      $brand = array();
      $week = array();
      $imgCnt = 1;
      $x = 1;
  
      echo "LIST OF JOBS ".'&lt;br/&gt;'.'&lt;br/&gt;'; 
      while($row = mysqli_fetch_array($result)){
          echo "Image Count".'&lt;br/&gt;';
           echo "$imgCnt".'&lt;br/&gt;';
          echo "Number".'&lt;br/&gt;';
          echo $x.'&lt;br/&gt;';
          echo "Jobs".'&lt;br/&gt;';
           echo $row['jobs'].'&lt;br/&gt;'.'&lt;br/&gt;'; 
           echo "Clients".'&lt;br/&gt;';
           echo $row['Client'].'&lt;br/&gt;'.'&lt;br/&gt;'; 
           echo "Brands".'&lt;br/&gt;';
           echo $row['Brand'].'&lt;br/&gt;'.'&lt;br/&gt;';
           echo "WEEK".'&lt;br/&gt;';
           echo $row['week'].'&lt;br/&gt;'.'&lt;br/&gt;';
  ?&gt;
  &lt;form style = '&lt;?php echo $imgCnt; ?&gt;' id='uploadForm-&lt;?php echo $imgCnt; ?&gt;' action = '' method = 'POST'&gt;
          &lt;input type="file"  class="image&lt;?php echo $imgCnt; ?&gt;" name="img" onChange="readURL(this);" /&gt;
          &lt;img id="blah" src="#" alt="your image" /&gt;&lt;br/&gt;&lt;br/&gt;
          &lt;input type='button' id = '&lt;?php echo $imgCnt; ?&gt;' class='uploadPicture&lt;?php echo $imgCnt; ?&gt; btn btn-primary' value = 'Upload'&gt;
          &lt;!-- &lt;input type="button" value="upload" class="uploadPicture"  id="upload_btn&lt;?php echo $imgCnt; ?&gt;"/&gt; --&gt;
  &lt;/form&gt;
  &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script&gt;
  function readURL(input) {      
          if (input.files &amp;&amp; input.files[0]) {
          var reader = new FileReader();
          reader.onload = function (e) {
              //$('#blah').attr('src', e.target.result).width(300).height(340);
               $(input).next('img').attr('src', e.target.result).width(300).height(340);
          };
          reader.readAsDataURL(input.files[0]);
      }
  
    }
  &lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  
         $('.uploadPicture&lt;?php echo $imgCnt; ?&gt;').unbind().click( function(e) { 
  
          var file_data = $('.image&lt;?php echo $imgCnt; ?&gt;').prop('files')[0];   
          var form_data = new FormData();                  
          form_data.append('file', file_data);
          var edit_id = $(this).attr("id");
          $.ajax({
                  url: "file.php",
                  dataType: 'text',  // what to expect back from the PHP script, if anything
                  cache: false,
                  contentType: false,
                  processData: false,
                  data: form_data,                         
                  type: 'post',
                  success: function (result) {
                          alert(result)
  
                  }
              });
          });
  
      &lt;/script&gt;
  &lt;?php
  echo "------------------------------------------------------------------------------------------------------".'&lt;br/&gt;';
  $x = $x + 1;
  $imgCnt++;
  }
  ?&gt;
  </code></pre>
  
  <p>Below is the PHP that is used to upload the image.</p>
  
  <pre><code>&lt;?php
  include('includes/config.php');
  $_SESSION['target'];
  $upload = 'uploads/';
  
  $img = $_POST['$imgCnt'];
  
  $sql = "SELECT * FROM tbl_job WHERE username = '$userid'";
  
  $result = mysqli_query($conn,$sql);
          $rowcount=mysqli_num_rows($result);
  
          while($row = mysqli_fetch_array($result)){
              if($img = 2){
              $target = $upload.'/'.$row['Client'].'/'.$row['Brand'].'/'.$row['jobs'].'/'.$row['week'].'/'.$row['store_code'].'/';
           //$_SESSION['target'] = $target; 
           if(!file_exists($target))
           {
               mkdir($target,0777,true);
           }       
           $imagePath = $upload.'/'.$row['Client'].'/'.$row['Brand'].'/'.$row['jobs'].'/'.$row['week'].'/'.$row['store_code'].'/';
  
  
      $temp = explode(".", $_FILES["file"]["name"]);
  
      $extension = end($temp);
  
      $filename = $_FILES["file"]["tmp_name"];
      echo "$filename".'&lt;br/&gt;';
      $time = time();
      move_uploaded_file($filename, $imagePath . $time . '.' . $extension);    
     echo "File Uploaded".'&lt;br/&gt;';
  
     echo "$img";
  
          }
  
          }
     exit;
   ?&gt;
  </code></pre>
  
  <p>Attached below is the screenshot of the page that shows the output on the first PHP.
  <a href="https://i.stack.imgur.com/g0UHm.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/g0UHm.jpg" alt="enter image description here"></a></p>
  
  <p>What I want to do is when the Image Count is 1 i want the picture to be uploaded to the folder created as:</p>
  
  <p>Client/Brand1/Job1/Week/StoreCode/image.jpg</p>
  
  <p>When the image count is 2,  i want the picture to be uploaded to the folder created as:</p>
  
  <p>Client/Brand2/Job2/Week/StoreCode/image.jpg</p>
  
  <p>The value of the Image count is posted from first PHP to the other. I just cannot make out how to use this value to move_uploaded_file to the respective folder.</p>