Page 1 of 1

file_get_contents problem

Posted: Wed Nov 22, 2006 2:21 am
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

Posted: Wed Nov 22, 2006 2:52 am
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);

Still doesn't work

Posted: Wed Nov 22, 2006 3:11 am
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

Posted: Wed Nov 22, 2006 3:54 am
by JayBird
Have you downloaded the PDF file via FTP to make sure the source isn't damaged.

Posted: Wed Nov 22, 2006 10:37 am
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

Posted: Wed Nov 22, 2006 12:07 pm
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