title: Failed to load PDF document in my PHP code
body: <p>I am new to PHP and trying to implement below functionality through code give</p>
  
  <p>Functionality: 
  I have one Download button and upon clicking i should be able to download my .pdf file stored under htdocs-->xxfilename-->abc.pdf;</p>
  
  <p>Code:
  I am using below code in my xyz.php web page </p>
  
  <pre><code>&lt;?php
  $title = "Learning";
  $content = '
              &lt;h3&gt; Intro&lt;/h3&gt;
              &lt;p&gt; 
                  Introduction goes here
              &lt;/p&gt;
              &lt;form action = "xyz.php" method = "post" name = "downloadform"&gt;
                  &lt;input type="submit" value="Download" name="dwnld_file"/&gt;
              &lt;/form&gt;
          ';
  if (isset($_POST['dwnld_file'])) {
      mysql_connect("localhost", "root", "");
      mysql_select_db("PQR");
      $res = mysql_query("Select * from tab1");
      while ($row = mysql_fetch_array($res)) {
          $file = '$row["col2"]';
          header('Content-Type: application/pdf');
          header('Content-Disposition: attachment; filename="' . $row["col2"] . '"');
          header('Content-Transfer-Encoding: binary');
          header('Accept-Ranges: bytes');
          header('Content-Length:' . filesize($file));
          readfile($file);
      }
  }
  include 'Template.php';
  ?&gt;
  </code></pre>
  
  <p>Error:
  My pdf file is getting downloaded but upon opening it it says "failed to load pdf document" </p>
  
  <p>Please help where i am wrong.</p>
  
  <p>***Edit****
  I tried different approach and my file is downloading but still it says "failed to load pdf document"</p>
  
  <p>My other approach code is below</p>
  
  <pre><code>&lt;form action = "xyz.php" method = "post" name = "downloadform"&gt;
          &lt;input type="submit" value="Download " name="dwnld_file"/&gt;
      &lt;/form&gt;
      &lt;?php
      if (isset($_POST['dwnld_file'])) {
          mysql_connect("localhost", "root", "");
          mysql_select_db("test");
          $res = mysql_query("Select * from tab1");
          while ($row = mysql_fetch_array($res)) {
              $file = $row["col1"];
              echo $file;
  
              header('Content-Type: application/pdf');
              header('Content-Disposition: attachment; filename="' .$file. '"');
              header('Content-Transfer-Encoding: binary');
              header('Accept-Ranges: bytes');
              header('Content-Length:' . filesize($file));
              readfile('myfile/'.$file);
          }
      }
      ?&gt;
  </code></pre>
  
  <p>Please tell me if i am doing anything wrong.</p>