[SOLVED] using headers to open files dynamically

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
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

[SOLVED] using headers to open files dynamically

Post by Burrito »

I have a database that is storing information about a "bank" of resource files for our company. I am using headers to decide what to do with the file:

for example, if it's an image file, they can use my "generic" page as the source of the image like so:

<img src="http://www.mydomain.com/files?fid=343"> <-- where fid is the id of the file on the database. It grabs the location of the file from the db, and then uses my headers to decide what to do with it. Here is my problem/question. If it's a pdf, I want it to just open in the browser if they visit the link like this: http://www.mydomain.com/files?fid=345. Right now it's asking if they want to save or open the file, if they select open, it doesn't know which program to open it with, but if they select save, it saves the file just find and they can open it from their machine locally. I want it to open pdfs within the browser itself.

here is the header information that I'm using:

Code: Select all

$size = filesize($gtfile['fileloc']);
		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: ".$content);
		$inora = ($in ? "inline" : "attachment");		
		$header="Content-Disposition: ".$inora."; filename=".$gtfile['fileloc'].";";
		header($header );
		header("Content-Transfer-Encoding: binary");
		header("Content-Length: ".$size);
		echo readfile($gtfile['fileloc']);
		exit;
I've tried both inline and attachment for pdfs and neither seem to work for what I'm trying to do...

any ideas?
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post by hongco »

Code: Select all

$fp = fopen($gtfile['fileloc'], "r");
header("Content-type: application/pdf");
fpassthru($fp);
fclose($fp);
using fpassthru will open up the pdf file onto your browser.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

It's not that.... my sources tell me that $gtfile['fileloc'] has a filepath with directories in it. Thats not a valid statement in the content-disposition header.

;-)

EDIT | It was the filepath with dirs in it. Talked to Burr on MSN and it's fixed
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

the other thing is that they have to have acrobat reader or some program to read .pdf files installed or it will never open in the browser.
Post Reply