title: Download File From MySQL DB
body: <p>I can't seem to download my files online which I'm uploading into a MySQL database I only get plain text, vague signs mostly. In localhost though downloading is no problem. I checked my database online and locally both are uploaded good in the upload table. I'll show you Upload.php(It's not called that in real, and this is also not the entire script, It's a big script) first:</p>
  
  <pre><code>if (isset($_POST['verstuur'])) {
  $id = NULL;
  if($_FILES['userfile']['size'] &gt; 0)
  {
  $fileName = $_FILES['userfile']['name'];
  $tmpName  = $_FILES['userfile']['tmp_name'];
  $fileSize = $_FILES['userfile']['size'];
  $fileType = $_FILES['userfile']['type'];
  
  $fp      = fopen($tmpName, 'r');
  $content = fread($fp, filesize($tmpName));
  $content = addslashes($content);
  fclose($fp);
  
  if(!get_magic_quotes_gpc())
  {
      $fileName = addslashes($fileName);
  }
  $query = "INSERT INTO upload (name, size, type, content ) ".
  "VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
  mysql_query($query) or die('Fout, query mislukt'); 
  $query = "SELECT LAST_INSERT_ID() ID";
  $result = mysql_query($query);
  while($row = mysql_fetch_array($result)){
  $id = $row['ID'];
  }
  </code></pre>
  
  <p>} </p>
  
  <p>Okay so I have another script where I create the download link:</p>
  
  <pre><code>$query = "SELECT U_ID, name FROM upload WHERE U_ID = '$opvraag[U_ID]'";
  $result = mysql_query($query) or die('Error, query failed');
  if(mysql_num_rows($result) == 0)
  {
   echo "&lt;td&gt;Geen Bijlage&lt;/td&gt;";
  } 
  else
  {
   while(list($id, $name) = mysql_fetch_array($result))
   {
       echo '&lt;td&gt;&lt;a href="download.php?id='.$opvraag['U_ID'].'"&gt;'.$name.'&lt;/a&gt;&lt;/td&gt;';
   }
  }
  </code></pre>
  
  <p>And Last I have download.php</p>
  
  <pre><code>if(isset($_GET['id'])) 
  {
  $id    = $_GET['id'];
  $query = "SELECT name, type, size, content " .
       "FROM upload WHERE U_ID = '$id'";
  $result = mysql_query($query) or die('Fout, query mislukt');
  list($name, $type, $size, $content) =  mysql_fetch_array($result);
  header("Content-length: $size");
  header("Content-type: $type");
  header("Content-Disposition: attachment; filename=$name");
  echo stripslashes($content);
  exit;
  }
  </code></pre>
  
  <p>The Database Table:<br>
  Name:     Type:<br>
  U_ID      int(8)              AUTO_INCREMENT<br>
  name      varchar(255)<br>
  size      int(8)<br>
  type      text<br>
  content   blob  </p>