Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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
Is there a way to send a binary file to the user's browser via PHP?

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   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';

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.


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.

$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.

<?php    // Demo - send a (binary) file

$file = "ireland.jpg";
$fp = fopen($file,"r") ;

header("Content-Type: image/jpeg");

while (! feof($fp)) {
       $buff = fread($fp,4096);
       print $buff;

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho