download php is always damaged
<p>I have a download script written in PHP. My view file script links to the ids and then selects all the data that matches the ID.</p>

<p>The data is then used to download the photo. Does it matter that my photo is in a folder? It is moved to a folder and then the directory is uploaded to the MYSQL database. </p>

<p>The code at the moment now allows some files to download perfectly and then the majority to be damaged. Any reason why?</p>

<pre><code>    Mysql table info...
    $cool = $_GET['id'];

  $sql = "SELECT id, type, name, size FROM upload WHERE id='$cool'";

   $result = mysql_query($sql, $db);
   $data = mysql_result($result, 0, "id");
   $name = mysql_result($result, 0, "name");
   $size = mysql_result($result, 0, "size");
   $type = mysql_result($result, 0, "type");

   header("Content-type: $type");
   header("Content-length: $size");
   header("Content-Disposition: attachment; filename=$name");
   header("Content-Description: PHP Generated Data");
   header('Content-Transfer-Encoding: binary');
   header('Expires: 0');
   header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
   header('Pragma: public');
   ob_clean();
   flush();
   readfile($name);
   exit();
</code></pre>
