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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a PDF File I get garbage

Status
Not open for further replies.

mevasquez

Programmer
Aug 26, 2003
75
US
Hi all,

I have read the readfile(), followed examples and I still get garbage when reading the PDF file. Here is what I have so far.

Code:
$filename = "doc6751.pdf";
header("Pragma: public");
header("Expires: 0");
header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header( "Content-type: application/pdf" );
header('Content-Length: ' . filesize($filename));
header("Content-Transfer-Encoding: binary");
readfile($filename) or die("Cannot open file");

The above code gives me garbage. I am using Firefox and I checked my plugins, about:plugins, and PDF was listed. I downloaded Live HTTP headers for Firefox and notice that the at the content-Type is still text/html.

Code:
GET /viewpdf.php?id=6751 HTTP/1.1
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cache-Control: max-age=0

HTTP/1.x 200 OK
Date: Tue, 01 May 2007 19:43:51 GMT
Server: Apache/2.0.52 (CentOS)
X-Powered-By: PHP/4.3.9
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

What do I need to do in order to view this document?
TIA
Mike
 
try adding an exit() as the line after readfile();

i am assuming that the file works just fine if you address it in your browser directly.

Content-Type should have initial caps. i don't know how relevant that is

add a header
Code:
header('Content-Disposition: attachment; filename="'.$filename.'"');
if it still doesn't work then try changing the content type to application/octet-stream
 
Still no luck.

If I just wanted to create a link and have the user download or open file, how is that done?
Mike
 
I cannot duplicate your error. I have tried Opera and IE, pulling the file from my LAMP box. I'm running your script, with the only difference being the PDF filename the script streams. Even without jpadie's advice of adding the Content-Disposition error, I am able to fetch the file and have it open in an Adobe plugin in the browser.

I do notice you're running PHP 4.3.9, which came out in September of 2004. Maybe your installations's buggy and an upgrade is in order. I'm running PHP 5.2.1



Want the best answers? Ask the best questions! TANSTAAFL!
 
I will try and upgrade php and get back to this question.

Thanks to all,

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top