title: How to download pdf files using php
body: <p>I want to download pdf files or any other file but my main motive is to download pdf files. Is it possible instead of download we can see the pdf files on browser when user submit a query.</p>
  
  <p>There is my upload code this works fine`</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&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 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;/body&gt;
  &lt;/html&gt;
  
  &lt;?php
  $uploadDir = 'C:\wamp\www\images\passport images';
  
  if(isset($_POST['upload']))
  {
  $fileName = $_FILES['userfile']['name'];
  $tmpName = $_FILES['userfile']['tmp_name'];
  $fileSize = $_FILES['userfile']['size'];
  $fileType = $_FILES['userfile']['type'];
  
  $filePath = $uploadDir . $fileName;
  
  $result = move_uploaded_file($tmpName, $filePath);
  if (!$result) {
  echo "Error uploading file";
  exit;
  }
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "db_dat";
  
  $conn = mysqli_connect($servername, $username, $password, $dbname);
  
  
  if(!get_magic_quotes_gpc())
  {
  $fileName = addslashes($fileName);
  $filePath = addslashes($filePath);
  }
  
  $query = "INSERT INTO upload2 (name, size, type, path ) VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";
  
  mysqli_query($conn,$query) or die('Error, query failed : ' . mysql_error());
  
  
  
  echo "&lt;br&gt;Files uploaded&lt;br&gt;";
  
  }
  ?&gt;
  </code></pre>
  
  <p><code>**Now problem in download code**. Actually I want user submit a query in text box acc. to that text as well as it's image which is scanned So in pdf format shows to him</code></p>
  
  <pre><code>&lt;?php
  if(isset($_GET['id']))
  {
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "db_dat";
  
  $conn = mysqli_connect($servername, $username, $password, $dbname);
  
  $id = $_GET['id'];
  $query = "SELECT name, type, size, path FROM upload2 WHERE id = '1'";
  $result = mysqli_query($conn,$query) or die('Error, query failed');
  list($name, $type, $size, $filePath) = mysqli_fetch_array($result);
  
  header("Content-Disposition: attachment; filename='".$name."'");
  header("Content-length: $size");
  header("Content-type: $type");
  
  echo "&lt;a href=" . $uploadDir .($row['userfile']) . "&gt;
           {$row['userfile']}&lt;/a&gt;";
  
  exit;
  }
  
   ?&gt;
  </code></pre>
  
  <p>`</p>