title: PHP upload to MySQL database with download and view file
body: <p>Does not download correctly: can't open the link. Help  appreciated. I am new to PHP and MySQL. I have MySQL set to BLOB for the content and I am not sure how to be clearer, I can see the link(s) for the file with the respective id to the file content $id in the url, but when I click on the link nothing opens up, I want to be able to open the file inthe brownser. I intend on being able to open .zip files and extract in later development. A sfar as security please also explain in good details so I can learn. I see my code was mod, but still not working in the array link. </p>
  
  <p><code>UPLOAD.PHP</code>:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php 
  $dbname="upload";
  $host="localhost";
  $user="SELF";
  $pass="PICME";
  $link = mysql_connect($hostname, $user, $pass);
  mysql_select_db($dbname, $link);
  ?&gt;
  &lt;form method="post" enctype="multipart/form-data"&gt;
     &lt;table width="350" border="0" cellpadding="1" cellspacing="1" class="box"&gt;
        &lt;tr&gt; 
           &lt;td width="246"&gt;
              &lt;input type="hidden" name="MAX_FILE_SIZE" value="2000000"&gt;
              &lt;input name="userfile" type="file" id="userfile"&gt; 
           &lt;/td&gt;
           &lt;td width="80"&gt;&lt;input name="upload" type="submit" class="box" id="upload" value=" Upload "&gt;&lt;/td&gt;
        &lt;/tr&gt;
     &lt;/table&gt;
  &lt;/form&gt;
  &lt;?php
  if(isset($_POST['upload']) &amp;&amp; $_FILES['userfile']['size'] &gt; 0)
  {
  $fileName = $_FILES['userfile']['name'];
  $tmpName  = $_FILES['userfile']['tmp_name'];
  $fileType = $_FILES['userfile']['type'];
  $fileSize = $_FILES['userfile']['size'];
  $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, type, size, content) ".
  "VALUES ('$fileName', '$fileType', '$fileSize', '$content')";
  mysql_query($query) or die('Error, query failed'); 
  echo "&lt;br&gt;File $fileName uploaded&lt;br&gt;";
  } 
  ?&gt;'
  (DOWNLOAD.PHP)FILE
  '&lt;?php
  $dbname="upload";
  $host="localhost";
  $user="SELF";
  $pass="PICME";
  $link = mysql_connect($hostname, $user, $pass);
  mysql_select_db($dbname, $link);
  $query = "SELECT id, name FROM upload";
  $result = mysql_query($query) or die('Error, query failed');
  if(mysql_num_rows($result) == 0)
  {
  echo "Database is empty &lt;br&gt;";
  } 
  else
  {
  while(list($id, $name) = mysql_fetch_array($result))
  {
  ?&gt;
  &lt;a href="download.php?id=&lt;?php echo urlencode($id);?&gt;"&gt;&lt;?php echo urlencode($name);?&gt;&lt;/a&gt; &lt;br&gt;
  &lt;?php 
  }
  }
  exit;
  ?&gt;
  &lt;?php
  $dbname="upload";
  $host="localhost";
  $user="SELF";
  $pass="PICME";
  $link = mysql_connect($hostname, $user, $pass);
  mysql_select_db($dbname, $link);
  $query = "SELECT id, name FROM upload";
  if(isset($_GET['id'])) 
  {
  // if id is set then get the file with the id from database
  $id    = $_GET['id'];
  $query = "SELECT name, type, size, content " .
  "FROM upload WHERE id = '$id'";
  $result = mysql_query($query) or die('Error, query failed');
  list($name, $type, $size, $content) =                     mysql_fetch_array($result);
  $content = $row['content']; 
  header("Content-Disposition: attachment; filename=$name");
  header('Content-type: image/jpeg' . $type); // 'image/jpeg' for JPEG images
  header('Content-Length:' . $size);
  exit;
  print $content;
  ob_clean();
  flush();
  echo $content;
  }
  ?&gt;
  </code></pre>