title: Unable to view the download PDF file in browser in php
body: <p>I have a download media function in which all type of media files gets downloaded. Below is the code</p>
  
  <pre><code>public function downloadMedia($file, $filename_direct = '', $extern = '', $exitHere = 1)
      {
          jimport('joomla.filesystem.file');
  
          clearstatcache();
  
          if (!$extern)
          {
              if (!JFile::exists($file))
              {
                  return 2;
              }
              else
              {
                  $len = filesize($file);
              }
          }
          else
          {
              /* Return the size of a remote url or a local file specified by $url.
                  $thereturn specifies the unit returned (either bytes "", MiB "mb" or KiB
                  "kb"). */
              $len = filesize($file);
          }
  
          $filename       = basename($file);
  
          $file_extension = strtolower(substr(strrchr($filename, "."), 1));
          $ctype = $this-&gt;getMime($file_extension);
  
          ob_end_clean();
  
          //  Needed for MS IE - otherwise content disposition is not used?
          if (ini_get('zlib.output_compression'))
          {
              ini_set('zlib.output_compression', 'Off');
          }
  
          header("Cache-Control: public, must-revalidate");
          header('Cache-Control: pre-check=0, post-check=0, max-age=0');
          header("Expires: 0");
          header("Content-Description: File Transfer");
          header("Content-Type: " . $ctype);
          header("Content-Length: " . (string) $len);
          header('Content-Disposition: attachment; filename="' . $filename . '"');
  
          //  set_time_limit doesn't work in safe mode
          if (!ini_get('safe_mode'))
          {
              @set_time_limit(0);
          }
  
          /*@readfile($file);
  
          if ($exitHere == 1)
          {
              exit;
          }*/
  
          $fp = fopen($file, "r") ;
  
          ob_clean();
          flush();
          while (!@feof($fp)) {
              $buff = @fread($fp, $len);
              print $buff;
          }
  
          exit;
      }
  </code></pre>
  
  <p>Now the issue is, whenever I download the PDF file &amp; clicked on downloaded file it shows an error in a browser like 'Failed to load PDF document.'</p>
  
  <p><a href="https://i.stack.imgur.com/DDNSB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DDNSB.png" alt="Error Image"></a></p>
  
  <p>The downloaded PDF file is opened correctly only when if I open the file in adobe instead of opening it in browser.</p>