Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error with opening pdf file script 1

Status
Not open for further replies.

sophielois

Technical User
Sep 8, 2005
66
GB
Hi all,

Im getting the following error when trying to open a pdf file using the follwong code

ERROR
!Acrobat could not open HSC21[1].pdf because it is not a supported file type or the file has been corrupted for example it was sent as an email attachment and wasn't correctly decoded

Code:
<?php

  function downloadFile($thefile, $ctype, $dname) {
      header("Pragma: public");
      header("Expires: 0");
      header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
      header("Cache-Control: public");
      header("Content-Description: File Transfer");
      header("Content-type: ".$ctype);
      header("Content-Length: ".filesize($thefile));
      header("Content-Disposition: attachment; filename=\"".$dname."\"");
      header("Content-Transfer-Encoding: binary");
         $fp = fopen($thefile);
     $txc = fpassthru($fp);
     fclose($fp);
  }

$thefile = "HSC21.pdf";
$ctype = "application/pdf";
$dname = "HSC21.pdf";

downloadFile($thefile, $ctype, $dname);
?>


Can you see where im going wrong?

Soph
 
can you try adding the "rb" mode to the fopen function. and lose th "$txc=" bit - just fpassthru($fp) is fine.

also if this still doesn't work, try making sure that auto_detect_line endings is enabled in your php ini file.
 
hi jpadie

thanks the following works
Code:
<?php

  function downloadFile($thefile, $ctype, $dname) {
      header("Pragma: public");
      header("Expires: 0");
      header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
      header("Cache-Control: public");
      header("Content-Description: File Transfer");
      header("Content-type: ".$ctype);
      header("Content-Length: ".filesize($thefile));
      header("Content-Disposition: attachment; filename=\"".$dname."\"");
      header("Content-Transfer-Encoding: binary");
       $fp = fopen($thefile, 'rb');
     fpassthru($fp);
     fclose($fp);
  }

$thefile = "HSC21.pdf";
$ctype = "application/pdf";
$dname = "HSC21.pdf";

downloadFile($thefile, $ctype, $dname);
?>

Coud you just explain what the "rb" mode does exactly?

thanks
Soph
 
the "r" means that it should open the file in read mode.

the "b" means ths it does so in a binary safe fashion. it's as well always to use the "b" as transferring text in binary mode is just fine too.

have a look at fopen in for all the different modes that exist (for writing, appending etc).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top