title: Getting Errors When Downloading Files From SQL Database
body: <p>I am running a website, and part of the site is allowing users to upload files to a SQL database, and then download them. The download itself works, but the file is getting corrupted.  Image files cannot be opened, doc files are showing up as blank.  I will attach below my uploading script and my downloading script. </p>
  
  <pre><code>  $classid = $_POST['uploadclass'];
  $userid = $_SESSION['id'];
  $view = $_POST['view']; 
  
  $filename = $_FILES['uploadfile']['name'];
  $tmpname = $_FILES['uploadfile']['tmpname'];
  $filesize = $_FILES['uploadfile']['size'];
  $filetype = $_FILES['uploadfile']['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 uploads VALUES('','$filename', '$filetype', '$filesize', '$content', '$userid', '$classid', '$view', 'no')";
  $run = mysqli_query($connect, $query);
  
  mysqli_close($connect);
  header('location: files.php');
  </code></pre>
  
  <p>Below is the code for my download page.</p>
  
  <pre><code>$query = "SELECT * FROM uploads WHERE id=$id";
  $run = mysqli_query($connect, $query);
  while ($row = mysqli_fetch_assoc($run)){
  $name = $row['name'];
  $type = $row['type'];
  $size = $row['size'];
  $content = $row['content'];
  
  }
  header("Content-length: $size");
  header("Content-type: $type");
  header("Content-Disposition: attachment; filename=$name");
  echo $content;
  ?&gt;
  </code></pre>