fopen() pdf file code being written and not showing pdf file

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
chandldj
Forum Newbie
Posts: 5
Joined: Mon Aug 22, 2005 7:28 pm

fopen() pdf file code being written and not showing pdf file

Post by chandldj »

I am trying to show a pdf file on a page by reading it and writing it to the page (I have my reasons for this versus an embed function or something).

instead of displaying a pdf file, the entire file code is being printed on the screen... how do i fix this???

Code: Select all

<?
header ("Content-Type: application/pdf");

$a = mysql_query($query);
$blah = mysql_fetch_assoc($a);
$case = $blah["caseaddress"];
echo " - case - ";
echo $case;

$case = $path.$case;

$chunksize = 1*(1024*1024); // how many bytes per chunk 
$buffer = ''; 
$handle = fopen($case, 'r+b');
if ($handle === false) 
	{ 
   return false; 
	} 
while (!feof($handle)) 
	{ 
	$buffer = fread($handle, $chunksize); 
	print $buffer; 
	} 
return fclose($handle);
?>

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your echo's of case need to be removed.

you can use readfile() too. :)
chandldj
Forum Newbie
Posts: 5
Joined: Mon Aug 22, 2005 7:28 pm

close, but no cigar

Post by chandldj »

Thanks for your help... can you tell i'm a newbie? hehe

i removed all echo commands from the code...

it printed up differently, but i'm still getting...

-----------------------------------------

%PDF-1.4 %äãÏÒ 3 0 obj <> stream xÚ+ä2P0·4RÈå234³rÀ, m%!......................

-----------------------------------------

just a huge page 'o code :(

any more ideas? i'm not sure if fopen is even the right function or if i'm using it right??? i put in readfile in place of fopen and same problem :(
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

seems like you're not sending the right headers, but you have one of the right headers above it... you could try other pdf headers.. you may need to send a content-length header using filesize()
chandldj
Forum Newbie
Posts: 5
Joined: Mon Aug 22, 2005 7:28 pm

got it! :)

Post by chandldj »

fyi...

the header was "Content-Type: "

when i put the "T" to lower case it worked... fussy fussy.

Thanks for your help!!!
Post Reply