php 下载文件 代码
$file_key = intval($_GET['f']);
if(isset ($file[$file_key]) && $file_key >0 ){
$down_file = $file[$file_key];
$my_path = $down_file['file_png'];//文件路径
$my_file = $down_file['file_name'];
header("Pragma: public"); //头文件信息
header("Expires: 0");
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0', false);
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
$browser = $_SERVER['HTTP_USER_AGENT']; //浏览器
//如果浏览器为IE
if(preg_match('/MSIE 5.5/', $browser) || preg_match('/MSIE 6.0/', $browser))
{
header('Pragma: private');
// the c in control is lowercase, didnt work for me with uppercase
header('Cache-control: private, must-revalidate');
// MUST be a number for IE
header("Content-Length: ".filesize($my_path));
header('Content-Type: application/x-download');
header('Content-Disposition: attachment; filename="'.$my_file.'"');
}
else //如果为其他浏览器
{
header("Content-Length: ".(string)(filesize($my_path)));
header('Content-Type: application/x-download');
header('Content-Disposition: attachment; filename="'.$my_file.'"');
}
header('Content-Transfer-Encoding: binary');
if(file_exists($my_path)){
if ($file = fopen($my_path, 'rb')) //打开文件
{
while(!feof($file) and (connection_status()==0)) //循环读取文件内容
{
//输出文件内容
print(fread($file,filesize($my_path)));
flush();
}
fclose($file); //关闭文件
}
}
exit;
}