0

My code is working fine. My files are downloading also but when I open one file then it is not opening giving an error "Error Failed to load PDF document."

    <?php

    $pno = $_GET['pno'];


   $sql = "SELECT file FROM tenders WHERE Tno = $id";


   $file = "data/" . $mysql_row['file '];

   header("Content-type:application/pdf");

  header("Content-Disposition:attachment;filename='downloaded.pdf'");

  readfile($file); 
  ?>
sana miraj
  • 25
  • 7

2 Answers2

0

I would recommend to always check if the file exists. If it doesn't readfile() would eventually put an error inside your pdf-file which may cause the problem. Try it like this:

$pno = $_GET['pno'];
$sql = "SELECT file FROM tenders WHERE Tno = $id";

$file = "data/" . $mysql_row['file '];

if(file_exists($file)){
    header("Content-type:application/pdf");
    header("Content-Disposition:attachment;filename='downloaded.pdf'");
    readfile($file); 
} else {
    echo "File does not exist!";
}

Also there is no declaration of the $id variable. Could it be that $pno should be changed to $id?

Gerrit Fries
  • 145
  • 1
  • 13
0
$file = "path_to_file";
$fp = fopen($file, "r") ;

header("Cache-Control: maxage=1");
header("Pragma: public");
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=".$myFileName."");
header("Content-Description: PHP Generated Data");
header("Content-Transfer-Encoding: binary");
header('Content-Length:' . filesize($file));
ob_clean();
flush();
while (!feof($fp)) {
  $buff = fread($fp, 1024);
  print $buff;
}
exit;
Aks
  • 21
  • 5