Page 1 of 1

[SOLVED] using headers to open files dynamically

Posted: Mon Apr 25, 2005 11:42 am
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?

Posted: Mon Apr 25, 2005 3:15 pm
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.

Posted: Mon Apr 25, 2005 4:05 pm
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

Posted: Tue Apr 26, 2005 3:27 am
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.