Web Hosting Talk







View Full Version : PHP fread() Question


iamkoa
01-07-2006, 07:09 PM
I pulled the following off of PHP.net and it seems to work great:


ob_end_start();

function send_file($name) {
global $location;

ob_end_clean();
$path = "../tmp/".$location."/".$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, "r")) {
while(!feof($file) and (connection_status()==0)) {
print(fread($file, 1024*8));
flush();
}
fclose($file);
}
return((connection_status()==0) and !connection_aborted());
}


This is how to call the funciton:



if (!send_file($filename)) {
// either the file transfer was incomplete
// or the file was not found
die ("File transfer failed.");
} else {
// the download was a success

echo "It worked!";
}


My file starts downloading as it should, but the "It worked!" message never shows. How can I execute code after a successful download?

Big thanks!