title: Store image upload in folder = to session id?
body: <p>i have a script that uploads photos to a directory 'file/'.
  I am wanting to make it so that within the 'file' directory there is another folder which is labelled the session id of the user logged in. so if user '1' uploads the photo it will go to 'file/1/default.jpg' and if user '2' 'file/2/default.jpg'.</p>
  
  <p>Can anyone please tell me how i might go about doing this. thanks.</p>
  
  <p>html:</p>
  
  <pre><code>&lt;input type="file" class="droparea spot" name="xfile" data-post="upload.php" data-width="180" data-height="250" data-crop="true"/&gt;
  
  
                  &lt;form id="sampleform" action="post.php" method="post"&gt;
  
                  &lt;/form&gt;
                  &lt;script&gt;
                      $('#sampleform').submit(function(e){
                          e.preventDefault();
                          $.ajax({
                              url:$(this).attr('action'),
                              type:'post',
                              data:$(this).serialize(),
                              dataType:'json',
                              success:function(r){
                                  $('#form-result').append(
                                  '&lt;div&gt;&lt;b&gt;Title: &lt;/b&gt;'+r.title+'&lt;/div&gt;'
                                  ,'&lt;div&gt;&lt;b&gt;Description: &lt;/b&gt;'+r.description+'&lt;/div&gt;'
                                  ,'&lt;div&gt;&lt;b&gt;Image/File: &lt;/b&gt;'
                                    +'&lt;a href="'+ r.url +'" target="_blank"&gt;'+ r.url +'&lt;/a&gt;'
                                    +'&lt;/div&gt;'
                                  );
                              }
                          });
                  });
                  &lt;/script&gt;
              &lt;/div&gt;
  
              &lt;script&gt;
              // Calling jQuery "droparea" plugin
              $('.droparea').droparea({
                  'init' : function(result){
                      //console.log('custom init',result);
                  },
                  'start' : function(area){
                      area.find('.error').remove(); 
                  },
                  'error' : function(result, input, area){
                      $('&lt;div class="error"&gt;').html(result.error).prependTo(area); 
                      return 0;
                      //console.log('custom error',result.error);
                  },
                  'complete' : function(result, file, input, area){
                      if((/image/i).test(file.type)){
                          area.find('img').remove();
                          //area.data('value',result.filename);
                          area.append($('&lt;img&gt;',{'src': result.path + result.filename + '?' + Math.random()}));
                      } 
                      //console.log('custom complete',result);
                  }
              });
              &lt;/script&gt;
  </code></pre>
  
  <p>upload.php:</p>
  
  <pre><code>&lt;?php
  
  // LOG
  $log = '=== ' . @date('Y-m-d H:i:s') . ' ===============================' . "\n"
          . 'FILES:' . print_r($_FILES, 1) . "\n"
          . 'POST:' . print_r($_POST, 1) . "\n";
  $fp = fopen('upload-log.txt', 'a');
  fwrite($fp, $log);
  fclose($fp);
  
  
  // Result object
  $r = new stdClass();
  // Result content type
  header('content-type: application/json');
  
  
  // Maximum file size
  $maxsize = 10; //Mb
  // File size control
  if ($_FILES['xfile']['size'] &gt; ($maxsize * 1048576)) {
      $r-&gt;error = "Max file size: $maxsize Kb";
  }
  
  
  // Uploading folder
  $folder = 'files/';
  if (!is_dir($folder))
      mkdir($folder);
  // If specifics folder 
  $folder .= $_POST['folder'] ? $_POST['folder'] . '/' : '';
  if (!is_dir($folder))
      mkdir($folder);
  
  
  // If the file is an image
  if (preg_match('/image/i', $_FILES['xfile']['type'])) {
  
      $filename = $_POST['value'] ? $_POST['value'] :
              $folder . '_default.jpg';
  } else {
  
      $tld = split(',', $_FILES['xfile']['name']);
      $tld = $tld[count($tld) - 1];
      $filename = $_POST['value'] ? $_POST['value'] :
              $folder . '_default.jpg';
  }
  
  
  // Supporting image file types
  $types = Array('image/png', 'image/gif', 'image/jpeg');
  // File type control
  if (in_array($_FILES['xfile']['type'], $types)) {
      // Create an unique file name    
      // Uploaded file source
      $source = file_get_contents($_FILES["xfile"]["tmp_name"]);
      // Image resize
      imageresize($source, $filename, $_POST['width'], $_POST['height'], $_POST['crop'], $_POST['quality']);
  } else
  // If the file is not an image
  if (in_array($_FILES['xfile']['type'], $types)) 
      move_uploaded_file($_FILES["xfile"]["tmp_name"], $filename);
  
  
  
  // File path
  $path = str_replace('upload.php', '', $_SERVER['SCRIPT_NAME']);
  
  // Result data
  $r-&gt;filename = $filename;
  $r-&gt;path = $path;
  $r-&gt;img = '&lt;img src="' . $path . $filename . '" alt="image" /&gt;';
  
  // Return to JSON
  echo json_encode($r);
  
  // Image resize function with php + gd2 lib
  function imageresize($source, $destination, $width = 0, $height = 0, $crop = false, $quality = 80) {
      $quality = $quality ? $quality : 80;
      $image = imagecreatefromstring($source);
      if ($image) {
          // Get dimensions
          $w = imagesx($image);
          $h = imagesy($image);
          if (($width &amp;&amp; $w &gt; $width) || ($height &amp;&amp; $h &gt; $height)) {
              $ratio = $w / $h;
              if (($ratio &gt;= 1 || $height == 0) &amp;&amp; $width &amp;&amp; !$crop) {
                  $new_height = $width / $ratio;
                  $new_width = $width;
              } elseif ($crop &amp;&amp; $ratio &lt;= ($width / $height)) {
                  $new_height = $width / $ratio;
                  $new_width = $width;
              } else {
                  $new_width = $height * $ratio;
                  $new_height = $height;
              }
          } else {
              $new_width = $w;
              $new_height = $h;
          }
          $x_mid = $new_width * .5;  //horizontal middle
          $y_mid = $new_height * .5; //vertical middle
          // Resample
          error_log('height: ' . $new_height . ' - width: ' . $new_width);
          $new = imagecreatetruecolor(round($new_width), round($new_height));
          imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $w, $h);
          // Crop
          if ($crop) {
              $crop = imagecreatetruecolor($width ? $width : $new_width, $height ? $height : $new_height);
              imagecopyresampled($crop, $new, 0, 0, ($x_mid - ($width * .5)), 0, $width, $height, $width, $height);
              //($y_mid - ($height * .5))
          }
          // Output
          // Enable interlancing [for progressive JPEG]
          imageinterlace($crop ? $crop : $new, true);
  
          $dext = strtolower(pathinfo($destination, PATHINFO_EXTENSION));
          if ($dext == '') {
              $dext = $ext;
              $destination .= '.' . $ext;
          }
          switch ($dext) {
              case 'jpeg':
              case 'jpg':
                  imagejpeg($crop ? $crop : $new, $destination, $quality);
                  break;
              case 'png':
                  $pngQuality = ($quality - 100) / 11.111111;
                  $pngQuality = round(abs($pngQuality));
                  imagepng($crop ? $crop : $new, $destination, $pngQuality);
                  break;
              case 'gif':
                  imagegif($crop ? $crop : $new, $destination);
                  break;
          }
          @imagedestroy($image);
          @imagedestroy($new);
          @imagedestroy($crop);
      }
  }
  
  ?&gt; 
  </code></pre>
  
  <p>post.php:</p>
  
  <pre><code>&lt;?php
  
  // LOG
  $log = '=== ' . @date('Y-m-d H:i:s') . ' ===============================' . "\n"
          . 'FILES:' . print_r($_FILES, 1) . "\n"
          . 'POST:' . print_r($_POST, 1) . "\n";
  $fp = fopen('post-log.txt', 'a');
  fwrite($fp, $log);
  fclose($fp);
  
  
  // Result object
  $r = new stdClass();
  // Result content type
  header('content-type: application/json');
  
  
  $data = $_POST['thumbnail'];
  unset($_POST['thumbnail']);
  
  if($data){
  
      // Uploading folder
      $folder = 'files/';
      if (!is_dir($folder))
          mkdir($folder);
      // If specifics folder 
      $folder .= $_POST['folder'] ? $_POST['folder'] . '/' : '';
      if (!is_dir($folder))
          mkdir($folder);
  
  
      $filename = $_POST['value'] ? $_POST['value'] :
              $folder . sha1(@microtime() . '-' . $_POST['name']) . '.jpg';
  
  
      $data = split(',', $data);
      file_put_contents($filename, base64_decode($data[1]));
  
  }
  die(json_encode(array_merge(array('url' =&gt; $filename), $_POST)));
  
  ?&gt;
  </code></pre>