file_get_contents problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jimthunderbird
Forum Contributor
Posts: 147
Joined: Tue Jul 04, 2006 3:59 am
Location: San Francisco, CA

file_get_contents problem

Post by jimthunderbird »

Hi All,
I uploaded a pdf files to the server and I try to output its content by using file_get_contens, here's my code:

Code: Select all

$content = file_get_contents("myfile.pdf");
        header("Content-type: application/pdf");
        header("Content-Disposition: attachment; filename=myfile.pdf");
        print $content;
But when acrobat reader try open it, it said the file is damaged...

Similar problem when I use the code above to read and output the content of a word document, it said the document has different encoding...

How do I get this to work correctly?

Don't have any hints on it, hope to hear from you all and thanks for helping.

With my best,
Jim
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

this is what i do

Code: Select all

$file = basename("myfile.pdf");
	$size = filesize("myfile.pdf");
	
	header("Content-Type: application/octet-stream");
	header("Content-Length: $size");
	
	// IE5.5 just downloads index.php if we don't do this
	
	if(preg_match("/MSIE 5.5/", $HTTP_USER_AGENT)) {
	
		   header("Content-Disposition: filename=$file");
	} else {
	
		   header("Content-Disposition: attachment; filename=$file");
	}
	header("Content-Transfer-Encoding: binary");
	$fh = fopen("myfile.pdf", "r");
	fpassthru($fh);
User avatar
jimthunderbird
Forum Contributor
Posts: 147
Joined: Tue Jul 04, 2006 3:59 am
Location: San Francisco, CA

Still doesn't work

Post by jimthunderbird »

Thanks for your help, but I try your code, still does not work. When Acrobat Reader open the pdf file, it said the file is damaged and could not be repaired...

With my best,
Jim
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Have you downloaded the PDF file via FTP to make sure the source isn't damaged.
User avatar
jimthunderbird
Forum Contributor
Posts: 147
Joined: Tue Jul 04, 2006 3:59 am
Location: San Francisco, CA

Post by jimthunderbird »

The file I'm pretty sure is ok since I can open it directly through it's link with acrobat reader. But when I try read its content using the code above it won't work correctly.

With my best,
Jim
User avatar
jimthunderbird
Forum Contributor
Posts: 147
Joined: Tue Jul 04, 2006 3:59 am
Location: San Francisco, CA

Post by jimthunderbird »

Hi,
I got the PDF working, but now getting problems with reading word documents..., still got the encoding problem when open with microsoft word.

With my best,
Jim
Post Reply