| |||||||||||
| |||||||||||
Sending PDF File to Browser via PHP Posted by bevelboy (bevelboy), 19 March 2003 We are attempting to write a secure content application that only serves up content to users within a given IP range or from a specified referer. We have it working properly where it checks the user's IP and referer and presents an HTML page (using include/require) from a secure directory in the file system (not beneath the web document root). This works very well, but we are now wondering if we can serve up other types of files like PDF, WAV, etc.The current transaction path is as follows: 1) User requests "secure" html page from web server. This page has a PHP verification page included within it before it sends any html. 2) The PHP verification page verifies the user's IP range or referer 3) if valid, it includes a remote html page from file system via PHP. If not valid, PHP script terminates and the user receives an error. Is there a way to send a binary file to the user's browser via PHP? Posted by admin (Graham Ellis), 20 March 2003 Quote:
Yes ... you can set the content type via the header function and send anything you like. There's an example of a jpg image being sent out in an earlier thread - see the archive at http://www.wellho.net/forum/1845515401.html. In that particular example, the ImageJPEG function does the actual ouput but there's nothing to stop you ouputting any binary data you like with a print. Posted by bevelboy (bevelboy), 20 March 2003 Thanks for the reply, Graham, (it's Dan in Malibu, BTW, hello).I'm very close to having this one solved, thanks to your help, but I have one final snag. I did a bit of searching to locate the correct content-type, and now I just need to know how to push the pdf binary into the variable. Here is the code of my included page: header("content-type: application/pdf"); $pdfdoc = '/file/system/path/pdftest.pdf'; print($pdfdoc); The second line is obviously where the problem is, as I don't know how to fill the variable with the pdf document. Silly, daft, lame, yes, all that, but I just can't seem to get it happening. Dan Posted by bevelboy (bevelboy), 21 March 2003 Here is my latest iteration. It does try to send the file to the browser, but it hoses and sends hundreds of "bad file handler" lines to the browser.$filename='/export/home/staff/dank/transfer/pdftest.pdf'; $fp=fopen($filename, "rb"); header( "Content-type: application/pdf"); while(!feof($fn)) { $buffer = fread($fn, 4096); print $buffer; } Posted by admin (Graham Ellis), 21 March 2003 Looks like you're opening a file on a resource handle called $fp, then testing / reading from a different name ($fn) - the sort of thing you can spend hours searching for!Here's a complete working example; I haven't bothered with the "b" in the fopen mode string, as this is only necessary on Windows and our servers are OS X or Linux. Code:
This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |