PHP output file on disk to browser

Related searches

I want to serve an existing file to the browser in PHP. I've seen examples about image/jpeg but that function seems to save a file to disk and you have to create a right sized image object first (or I just don't understand it :))

In I do it by reading the file in a byte array and then call context.Response.BinaryWrite(bytearray), so I'm looking for something similar in PHP.


There is fpassthru() that should do exactly what you need. See the manual entry to read about the following example:


// open the file in a binary mode
$name = './img/ok.png';
$fp = fopen($name, 'rb');

// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));

// dump the picture and stop the script


See here for all of PHP's filesystem functions.

If it's a binary file you want to offer for download, you probably also want to send the right headers so the "Save as.." dialog pops up. See the 1st answer to this question for a good example on what headers to send.

readfile - Manual, readfile() will not present any memory issues, even when sending large files, on its Some browsers may work without quotation, but for sure not Firefox and as 'echo fread($fp, 8192)' in a loop, and readfile is even faster for files on disk. If you know the target _can't_ be a remote file (e.g. prefixing it with a directory), you should use include instead. If the user manages to set the target to some kinda config-file (configuration.php in Joomla!), he will get a blank page - unless readfile() is used. Using include will just behave as a normal request (no output).

I use this

  if (file_exists($file)) {

        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));



file_get_contents - Manual, file_get_contents() is the preferred way to read the contents of a file into a string. Example #1 Get and output the source of the homepage of a website. <?php but you can force it, especially if you are trying to act as a web browser. I don't� Parameters. directory. A directory of the filesystem or disk partition. Note: . Given a file name instead of a directory, the behaviour of the function is unspecified and may differ between operating systems and PHP versions.

I use readfile() ( )...

But you have to make sure you set the right "Content-Type" with header() so the browser knows what to do with the file.

You can also force the browser to download the file instead of trying to use a plug-in to display it (like for PDFs), I always found this to look a bit "hacky", but it is explained at the above link.

How to Force Download Files Using PHP, In this tutorial you will learn how to force download a file using PHP. whenever a user click on the link, browser will automatically downloads that file. kind of files to download directly to the user's hard drive using the PHP readfile() function . For example, if you have login information before the test.php, it will not get passed into the file if you call it with fopen. If you need it genuinely included, then what you have is the simplest method, but if you want constant output, you'll need to actually write test.php in a manner that outputs as well as stores the information as it goes.

This should get you started:

Edit: If your web server supports it, using

header('X-Sendfile: ' . $filename);

where file name contains a local path like


is faster than readfile().

(usual security considerations for using header() apply)

How to open a PDF files in web browser using PHP?, PHP uses a standard code to display the pdf file in web browser. Output: Example 2: this examples displays a format and explains every� The get_browser() function looks up the user's browscap.ini file and returns the capabilities of the user's browser. Syntax get_browser( user_agent,return_array )

For both my website and websites I create for clients I use a PHP script that I found a long time ago.

It can be found here:

I use a slightly modified version of it to allow me to obfuscate the file system structure (which it does by default) in addition to not allowing hot linking (default) and I added some additional tracking features, such as referrer, IP (default), and other such data that I might need should something come up.

Hope this helps.

The reason is PHP files first need be processed in a web server before sending their output to the web browser. Therefore before running PHP files, they should be placed inside the web folder of a web server and then make a request to desired PHP file by typing its URL in the web browser.

Looks like any specified resolution settings in a JPEG file get lost when using imageJPEG() function in a script. I had a high resolution JPEG which i added text to with imagestringup() so my customer can print the emailed JPEG as a filled form, but printing both Graphics from Photoshop revealed that any JPEG created is downsampled to 72 dpi, while width and height is kept.

php://stdin, php://stdout and php://stderr allow direct access to the corresponding input or output stream of the PHP process. The stream references a duplicate file descriptor, so if you open php://stdin and later close it, you close only your copy of the descriptor-the actual stream referenced by STDIN is unaffected.

Php might be buffering the output due to some settings or something similar. Then it needs to be told not to do that. Next comes the apache or any other web server which might buffer the output if for example it is configured to compress the output. Last one is the browser and it too might buffer the output before actually rendering it on the

  • What if I already have binary content stored as a variable. How do I output it to the browser as a downloadable file?
  • Thanks for the script. If i want to expand my first script i'll definately will use it.