PHP corrupt file upload
<p>I'm trying to let users upload files onto my website, but unfortunately some of them seem to turn corrupt when reading them. I've tried both images and html files, and all the images come through corrupt (the HTML files come through fine).</p>

<p>To upload the files I'm using a standard HTML form and the PHP $_FILES array. I'm then using the following code to read the contents of the file:</p>

<blockquote>
  <p>$filename = $_FILES['varname']['tmp_name'];</p>
  
  <p>$handle = fopen($filename, "r");`</p>
  
  <p>$contents = fread($handle, filesize($filename));</p>
  
  <p>fclose($handle);</p>
</blockquote>

<p>Unfortunately the value of $contents is now slightly different to the file I uploaded (here's a snippet from the top of the file): </p>

<p><strong>Original file:</strong></p>

<blockquote>
  <p>ˇÿˇ·ExifII*ˇÏDucky&lt;ˇÓAdobed¿ˇ€Ñ       </p>
</blockquote>

<p><strong>New file:</strong></p>

<blockquote>
  <p>ˇÿˇ· Exif  II*            ˇÏ Ducky     &lt;  ˇÓ Adobe d¿   ˇ€ Ñ </p>
</blockquote>

<p>As you can see there's a difference in the spacing. Any ideas what would be causing this? Am I handling the file read incorrectly for binary files? It seems odd that it's fine for any text files I upload..</p>

<p>Thanks!</p>
