PHP output file on disk to browser

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

<?php

// 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
fpassthru($fp);
exit;

?>

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.

Leave a Comment