note 102175 added to function.header
| From: | phpnet at holodyn dot com | Date: | Mon, 31 Jan 2011 21:01:23 +0000 |
| Subject: | note 102175 added to function.header | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-176545@lists.php.net to get a copy of this message | ||
I've written this function so many times, but every time I find a new problem - this one is
working in all browsers.
function downloadFile( $fullPath ){
// Must be fresh start
if( headers_sent() )
die('Headers Sent');
// Required for some browsers
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
// File Exists?
if( file_exists($fullPath) ){
// Parse Info / Get Extension
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
// Determine Content Type
switch ($ext) {
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
header("Content-Disposition: attachment;
filename=\"".basename($fullPath)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$fsize);
ob_clean();
flush();
readfile( $fullPath );
} else
die('File Not Found');
}
----
Server IP: 69.147.83.197
Probable Submitter: 71.204.87.158
----
Manual Page -- http://www.php.net/manual/en/function.header.php
Edit -- https://master.php.net/note/edit/102175
Del: integrated -- https://master.php.net/note/delete/102175/integrated
Del: useless -- https://master.php.net/note/delete/102175/useless
Del: bad code -- https://master.php.net/note/delete/102175/bad+code
Del: spam -- https://master.php.net/note/delete/102175/spam
Del: non-english -- https://master.php.net/note/delete/102175/non-english
Del: in docs -- https://master.php.net/note/delete/102175/in+docs
Del: other reasons-- https://master.php.net/note/delete/102175
Reject -- https://master.php.net/note/reject/102175
Search -- https://master.php.net/manage/user-notes.php