note 49078 added to function.fread
| From: | dot) | Date: | Sun, 16 Jan 2005 08:20:46 +0000 |
| Subject: | note 49078 added to function.fread | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-83397@lists.php.net to get a copy of this message | ||
Here's a function for sending a file to the client - it may look more complicated than
necessary, but has a number of advantages over simpler file sending functions:
- Works with large files, and uses only an 8KB buffer per transfer.
- Stops transferring if the client is disconnected (unlike many scripts, that continue to read and
buffer the entire file, wasting valuable resources) but does not halt the script
- Returns TRUE if transfer was completed, or FALSE if the client was disconnected before completing
the download - you'll often need this, so you can log downloads correctly.
- Sends a number of headers, including ones that ensure it's cached for a maximum of 2 hours on
any browser/proxy, and "Content-Length" which most people seem to forget.
(tested on Linux (Apache) and Windows (IIS5/6) under PHP4.3.x)
Note that the folder from which protected files will be pulled, is set as a constant in this
function (/protected) ... Now here's the function:
<?php
function send_file($name) {
ob_end_clean();
$path = "protected/".$name;
if (!is_file($path) or connection_status()!=0) return(FALSE);
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2,
date("i"), date("s"), date("m"), date("d"),
date("Y")))." GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Content-Type: application/octet-stream");
header("Content-Length: ".(string)(filesize($path)));
header("Content-Disposition: inline; filename=$name");
header("Content-Transfer-Encoding: binary\n");
if ($file = fopen($path, 'rb')) {
while(!feof($file) and (connection_status()==0)) {
print(fread($file, 1024*8));
flush();
}
fclose($file);
}
return((connection_status()==0) and !connection_aborted());
}
?>
And here's an example of using the function:
<?php
if (!send_file("platinumdemo.zip")) {
die ("file transfer failed");
// either the file transfer was incomplete
// or the file was not found
} else {
// the download was a success
// log, or do whatever else
}
?>
Regards,
Rasmus Schultz
----
Manual Page -- http://www.php.net/manual/en/function.fread.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+49078
Delete -- http://master.php.net/manage/user-notes.php?action=delete+49078&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49078&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+49078&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+49078&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+49078&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+49078&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+49078&report=yes
Search -- http://master.php.net/manage/user-notes.php