title: getimagesize() on stream instead of string
body: <p>I'm using <a href="http://valums.com/ajax-upload/" rel="nofollow">Valum's file uploader</a> to upload images with AJAX. This script submits the file to my server in a way that I don't fully understand, so it's probably best to explain by showing my server-side code:</p>
  
  <pre><code>$pathToFile = $path . $filename;
  
  //Here I get a file not found error, because the file is not yet at this address
  getimagesize($pathToFile);
  
  $input = fopen('php://input', 'r');
  $temp = tmpfile();
  $realSize = stream_copy_to_stream($input, $temp);
  
  //Here I get a string expected, resource given error 
  getimagesize($input);
  
  fclose($input);
  
  $target = fopen($pathToFile, 'w');
  fseek($temp, 0, SEEK_SET);
  
  //Here I get a file not found error, because the image is not at the $target yet
  getimagesize($pathToFile);
  
  stream_copy_to_stream($temp, $target);
  fclose($target);
  
  //Here it works, because the image is at the desired location so I'm able to access it with $pathToFile. However, the (potentially) malicious file is already in my server.
  getimagesize($pathToFile);
  </code></pre>
  
  <p>The problem is that I want to perform some file validation here, using getimagesize(). getimagesize only supports a string, and I only have resources available, which result in the error: getimagesize expects a string, resource given.</p>
  
  <p>It does work when I perform getimagesize($pathTofile) at the end of the script, but then the image is already uploaded and the damage could already have been done. Doing this and performing the check afterwards and then maybe deleting te file seems like bad practice to me.</p>
  
  <p>The only thing thats in $_REQUEST is the filename, which i use for the var $pathToFile. $_FILES is empty.</p>
  
  <p>How can I perform file validation on streams?</p>
  
  <p><strong>EDIT:</strong> 
  the solution is to first place the file in a temporary directory, and perform the validation on the temporary file before copying it to the destination directory.</p>
  
  <pre><code>// Store the file in tmp dir, to validate it before storing it in destination dir
  $input = fopen('php://input', 'r');
  $tmpPath = tempnam(sys_get_temp_dir(), 'upl'); // upl is 3-letter prefix for upload
  $tmpStream = fopen($tmpPath, 'w'); // For writing it to tmp dir
  stream_copy_to_stream($input, $tmpStream);
  fclose($input);
  fclose($tmpStream);
  
  // Store the file in destination dir, after validation
  $pathToFile = $path . $filename;
  $destination = fopen($pathToFile, 'w');
  $tmpStream = fopen($tmpPath, 'r'); // For reading it from tmp dir
  stream_copy_to_stream($tmpStream, $destination);
  fclose($destination);
  fclose($tmpStream);
  </code></pre>