note 77859 added to function.header
| From: | yo3hcv at yahoo dot com | Date: | Sun, 16 Sep 2007 18:42:24 +0000 |
| Subject: | note 77859 added to function.header | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130540@lists.php.net to get a copy of this message | ||
After spent a lot of time testing with http sniffers, I (think) getâ it multi-browser work for
force-download. Especially IE was really pain in the ass, thanks Microsoft.
When called, following function brings a "Open or Save" dialog in user browser, actually
sending requested file and hiding originally file path. When Open, appropriate application starts
automatically loaded with file. I put my files in some htaccess protected directory, so smart users
cannot download them directly, just served by my script seen as 'localhost'.
My htaccess file content
Order Deny,Allow
Deny From All
Allow From 127.0.0.1
Take care with function like this one, left unprotected :) smart users can get ANY file from your
server.
function send_file($file) {
ob_end_clean();
if (!is_file($file) or connection_status()!=0) return(FALSE);
if (strpos($file, '.gif',1)||strpos($file, '.gif',1) ) $content =
"image/gif";
else if (strpos($file, '.jpg',1)||strpos($file, '.jpg',1) ) $content =
"image/jpg";
else if (strpos($file, '.png',1)||strpos($file, '.png',1) ) $content =
"image/png";
else if (strpos($file, '.exe',1)||strpos($file, '.exe',1) ) $content =
"application/octet-stream";
else if (strpos($file, '.pdf',1)||strpos($file, '.pdf',1) ) $content =
"application/pdf";
else if (strpos($file, '.zip',1)||strpos($file, '.zip',1) ) $content =
"application/zip";
else if (strpos($file, '.rar',1)||strpos($file, '.rar',1) ) $content =
"application/rar";
// any MIME you may need...
else $content = "application/force-download";
// without following line, IE goes crazy on Content-Disposition :)
// (wonât recognize full path, only the file)
header("Cache-Control: must-revalidate");
// 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("Expires: 0");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Content-Type: $content");
header("Content-Length: ".(string)(filesize($file)));
header("Content-Disposition: attachment; filename=".basename($file)."" );
header("Content-Transfer-Encoding: binary\n");
header ('Connection: close');
if ($fp = fopen($file, 'rb')) {
while(!feof($fp) and (connection_status()==0)) {
print(fread($fp, 1024*8));
flush();
}
fclose($fp);
}
return((connection_status()==0) and !connection_aborted());
}
Tested on IE (XP SP2), Mozilla 1.7.11 and Firefox 2.0.0.6
Cheers,
Edi
----
Server IP: 69.147.83.197
Probable Submitter: 86.55.6.246 (proxied: 89.37.71.16)
----
Manual Page -- http://www.php.net/manual/en/function.header.php
Edit -- https://master.php.net/note/edit/77859
Del: integrated -- https://master.php.net/note/delete/77859/integrated
Del: useless -- https://master.php.net/note/delete/77859/useless
Del: bad code -- https://master.php.net/note/delete/77859/bad+code
Del: spam -- https://master.php.net/note/delete/77859/spam
Del: non-english -- https://master.php.net/note/delete/77859/non-english
Del: in docs -- https://master.php.net/note/delete/77859/in+docs
Del: other reasons-- https://master.php.net/note/delete/77859
Reject -- https://master.php.net/note/reject/77859
Search -- https://master.php.net/manage/user-notes.php