<?php
/* PHP文件下载函数,隐藏文件真实链接地址,直接通过http headers将文件内容发送到浏览器转存到访问电脑
* 吴先成 2013-12-13 11:57:49 GMT+8 www.51-n.com
* @param $dir 文件存放目录
* @param $filename 文件名
* @param $isU8 页面是否是utf-8编码,默认为true,为真时将utf-8转为gbk,以避免文件系统函数出错
关于统一编码的更多细节请访问 http://www.51-n.com/t-4006-1-1.html
获取最新版 PHP 5.5.7 请访问 http://www.51-n.com/t-4099-1-1.html
*/
function download($dir,$filename,$isU8=true){
$fullPath = $dir.'/'.$filename;
if($isU8) $fullPath = @iconv('utf-8','gbk//ignore',$fullPath);
if(!is_file($fullPath)) return false;
if(!$f = @fopen($fullPath,'r')) return false;
$fstat = fstat($f);
ob_clean();
header('Content-Type:application/octet-stream');
header('Content-Disposition:attachment;filename='.$filename);
Header('Accept-Ranges:bytes');
Header('Accept-Length:'.$fstat['size']); //附件尺寸
header('Content-Transfer-Encoding:binary');
header('Connection:close');
while(!feof($f)){
echo fread($f, 10240);
}
fclose($f);
}
//使用实例
download('c:/windows/system32/','cmd.exe');
?>
|
|