title: php mysql download link
body: <p>On this page, a user picks some options from drop downs and receives a list of files available for download.  Download hyperlink next to each record.  Currently the download hyperlink downloads a file with the correct FILE NAME as it exists in the database, but the file CONTENTS are incorrect. More specifically, it seems like a header is not being sent correctly: The contents of the file is the html markup from newpub_profile.php </p>
  
  <p>This is the most current code:  </p>
  
  <p>newpub_profile.php</p>
  
  <pre><code>       $q = "SELECT upload_id, title, genre, length, created
          FROM upload
          WHERE genre = '$genre' AND length = '$length'
          ORDER BY created DESC, title DESC";
  
  
      $r = mysqli_query ($dbc, $q); // Run the query
  
      if($r)
      {
          // If it ran okay, display the records
          echo '&lt;table align="center"
              cellspacing="3" cellpadding="3"
              width="75%"&gt;
             &lt;tr&gt;&lt;td align="left"&gt;&lt;b&gt;Title&lt;/b&gt;&lt;/td&gt;
             &lt;td align="left"&gt;&lt;b&gt;Genre&lt;/b&gt;&lt;/td&gt;
             &lt;td align="left"&gt;&lt;b&gt;Pages&lt;/b&gt;&lt;/td&gt;
             &lt;td align="left"&gt;&lt;b&gt;Submitted&lt;/b&gt;&lt;/td&gt;
             &lt;td align="left"&gt;&lt;b&gt;Download&lt;/b&gt;&lt;/td&gt;';
  
          // Fetch and print all the records:
  
          while ($row = mysqli_fetch_array($r,MYSQLI_ASSOC))
          {
              echo '&lt;tr&gt;&lt;td align="left"&gt;' .
              $row['title'] . '&lt;/td&gt;&lt;td align="left"&gt;'
              . $row['genre'] . '&lt;/td&gt;&lt;td align="left"&gt;'
              . $row['length'] . '&lt;/td&gt;&lt;td align="left"&gt;'
              . $row['created'] . '&lt;/td&gt;&lt;td align="left"&gt;'
              //. $row['views'] . '&lt;/td&gt;&lt;td align="left"&gt;'
              . "&lt;a href='newpub_profile.php?id={$row['upload_id']}'&gt;Download&lt;/a&gt;&lt;/td&gt;" . '&lt;/td&gt;&lt;/tr&gt;';
          }
          echo '&lt;/table&gt;'; // Close the table
  
          mysqli_free_result ($r); // Free up the resources
      }
      else // If it did not run okay
      {
          // Public Message:
  
          echo '&lt;p class="error"&gt;Your submissions could not be retrieved.  We
              apologize for any inconvenience.&lt;/p&gt;';
  
          // Debugging message:
  
          echo '&lt;p&gt;' . mysqli_error($dbc) . '&lt;br /&gt;&lt;br /&gt;Query: ' . $q . '&lt;/p&gt;';
  
      } // End of if ($r) IF.
  
  
  
  }
  
  //END DOWNLOAD HANDLER ******************************************************
  
  
      mysqli_close($dbc); // Close the database connection
  
  ?&gt;
                      &lt;?php
  
                      // Make sure an ID was passed DOWNLOAD HANDLER *******
  if(isset($_GET['id'])) {
  // Get the ID
      $id = intval($_GET['id']); var_dump($id);
  
  
      require_once ('../mysqli_connect.php'); //Connect to the db
  
  
  
          // Fetch the file information
          $downloadq = "
              SELECT `file_type`, `size`, `title`, 'content', 'upload_id'
              FROM `upload`
              WHERE `upload_id` =".$id;
          $result = mysqli_query ($dbc, $downloadq); // Run the query
  
  
          if($result) {
              // Make sure the result is valid
              if (mysqli_num_rows($result) &gt; 0) {
              // Get the row
                  $row = mysqli_fetch_assoc($result);
                  //var_dump($row);
  
                  // Print headers
                  header("Content-Type: ". $row['type']);
                  header("Content-Length: ". $row['size']);
                 header("Content-Disposition: attachment; filename=". $row['title']);
  
  
                  // Print data
                  echo stripslashes($row['content']);
              }
              else {
                  echo 'Error! No such ID.';
              }
  
              // Free the mysqli resources
              mysqli_free_result($result);
          }
          else {
              echo "Error! Query failed: &lt;pre&gt;{$dbc-&gt;error}&lt;/pre&gt;";
          }
          mysqli_close($dbc);
      }
  
                      ?&gt;
  </code></pre>
  
  <p>newupload_sql.php</p>
  
  <pre><code>        if(isset($_POST['upload']) &amp;&amp; $_FILES['userfile']['size'] &gt; 0) //check membership level here*******************************************
  {
    //------------------------------------------------------------------------------------------------
      //$membercheck = "SELECT membership, uploaded
                     // FROM user
                     // WHERE user_id =". $_SESSION['user_id'];  //gets membership level and upload count
  
     //-----------------------------------------------------------------------------------------------
  
      if($_FILES['userfile']['size'] &gt; 2621440)
          die("File larger than 2.5MB");
  
         $mimeTypes = array('application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  'application/msword');
  
      if (in_array($_FILES['userfile']['type'], $mimeTypes))
        {
  $fileName = $_FILES['userfile']['name'];
  $tmpName  = $_FILES['userfile']['tmp_name'];
  $fileSize = $_FILES['userfile']['size'];
  $fileType = $_FILES['userfile']['type'];
  $oid = $_SESSION['user_id'];
  $views = 0;
  
  
  $fp      = fopen($fileName, 'r');
  $content = fread($fp, filesize($fileName));
  $content = addslashes($content);
  fclose($fp);
  
  //if(!get_magic_quotes_gpc())
  //{
    //  $fileName = addslashes($fileName);
  //}
  
  $query = "INSERT INTO upload (title, file_type, size, content, length, genre, created, views, description, owner_id) ".
  "VALUES ('$fileName', '$fileType','$fileSize', '$content', '$length', '$genre', NOW(), '$views', '$description', '$oid')";
  $r = @mysqli_query ($dbc, $query); //Run the query. or die('Error, query failed');
  
  if($r)
  {
  
          require_once ('login_functions.php');
          $url = absolute_url ('newupload_thanks.php');
          header("Location: $url");
          exit();
  }
  
              else //if it didnt run ok...
              {
                  //Public message:
                  echo '&lt;h1&gt;System Error&lt;/h1&gt;
                  &lt;p class="error"&gt;You could not upload due to a system
                  error.  We apologize.&lt;/p&gt;';
  
                  //debugging message:
  
                  echo'&lt;p&gt;' . mysqli_error($dbc) . '&lt;br /&gt;&lt;br /&gt;Query: ' . $r .
                  '&lt;/p&gt;';
  
              } // End of ($r) IF. // File's OK
      }
  
  }
    else
      {
          die("Wrong tile type: Use .doc, .docx or ONLY");
  
      }
  
  
  
  }mysqli_close($dbc); //Close the database connection.
  ?&gt;
  </code></pre>