title: Upload and Download Php MySQL Script
body: <p>I've made use of a script which is available online. The File upload.php allows the user to upload a file and then store the selected file in the MySQL database. Later the download.php script displays the links for all the files stored in the database. When the user clicks the link, the file should be downloaded. I've enclosed the script below. But the problem is, when I click the link the content of the file gets displayed instead of getting downloaded.</p>
  
  <h2>upload.php</h2>
  
  <pre><code>&lt;!--
  
  CREATE TABLE IF NOT EXISTS `upload` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(30) NOT NULL,
    `type` varchar(30) NOT NULL,
    `size` int(11) NOT NULL,
    `content` longblob NOT NULL,
    PRIMARY KEY (`id`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
  
  
  --&gt;
  &lt;html&gt;
      &lt;head&gt;&lt;/head&gt;
      &lt;body&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&gt;please select a file&lt;/td&gt;&lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;
                          &lt;input type="hidden" name="MAX_FILE_SIZE"
                                 value="16000000"&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;/body&gt;
  &lt;/html&gt;
  
  &lt;?php
  if (isset($_POST['upload']) &amp;&amp; $_FILES['userfile']['size'] &gt; 0) {
      $fileName = $_FILES['userfile']['name'];
      $tmpName = $_FILES['userfile']['tmp_name'];
      $fileSize = $_FILES['userfile']['size'];
      $fileType = $_FILES['userfile']['type'];
      $fileType = (get_magic_quotes_gpc() == 0 ? mysql_real_escape_string(
                              $_FILES['userfile']['type']) : mysql_real_escape_string(
                              stripslashes($_FILES['userfile'])));
      $fp = fopen($tmpName, 'r');
      $content = fread($fp, filesize($tmpName));
      $content = addslashes($content);
      fclose($fp);
      if (!get_magic_quotes_gpc()) {
          $fileName = addslashes($fileName);
      }
      $con = mysql_connect('localhost', 'root', 'root') or die(mysql_error());
      $db = mysql_select_db('test', $con);
      if ($db) {
          $query = "INSERT INTO upload (name, size, type, content ) " .
                  "VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
          mysql_query($query) or die('Error, query failed');
          mysql_close();
          echo "&lt;br&gt;File $fileName uploaded&lt;br&gt;";
      } else {
          echo "file upload failed";
      }
  }
  ?&gt;
  </code></pre>
  
  <h2>Download.php</h2>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Download File From MySQL Database&lt;/title&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; 
                charset=iso-8859-1"&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;?php
          $con = mysql_connect('localhost', 'root', 'root') or die(mysql_error());
          $db = mysql_select_db('test', $con);
          $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
              }
          }
          mysql_close();
          ?&gt;
      &lt;/body&gt;
  &lt;/html&gt;
             &lt;?php
             if (isset($_GET['id'])) {
                 $con = mysql_connect('localhost', 'root', 'root') or die(mysql_error());
                 $db = mysql_select_db('test', $con);
                 $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);
                 header("Content-length: $size");
                 header("Content-type: $type");
                 header("Content-Disposition: attachment; filename=$name");
                 ob_clean();
                 flush();
                 echo $content;
                 mysql_close();
                 exit;
             }
             ?&gt;
  </code></pre>