前言
这是一个简单的文件下载案例,可供新手参考学习,欢迎交流
一、文件下载
代码如下(示例):
public function bigFileDownLoad($down_path, $down_name) {
// 检测文件是否存在、是否可读
if (file_exists($down_path) && is_readable($down_path)) {
// 避免下载超时
set_time_limit(0);
// 避免中文乱码
// iconv编码转换存在bug, $down_name = iconv('utf-8', 'gb2312', $down_name);
$down_name = mb_convert_encoding($down_name, 'gb2312', 'utf-8');
// 打开
$fp = fopen($down_path, 'rb');
// 文件大小
/*$filesize = filesize($down_path);*/
// 返回的文件(流形式)
// 按照字节大小返回
header("Content-type: application/octet-stream");
// 返回文件大小
header("Content-Ranges: bytes");
/*header("Content-Length: $filesize");*/
// 这里客户端的弹出对话框,对应的文件名
header("Content-Disposition: attachment; filename=" . $down_name);
// 读取长度
$buffer = 8192;
// 循环读取
while (!feof($fp)) {
echo fread($fp, $buffer);
@ob_flush();
@flush();
}
// 关闭
fclose($fp);
}
}
总结
提示:这是个简单的小案例,仅供新手参考学习,如果问题欢迎交流。