php实现大文件下载且隐藏文件真实地址
2017-10-23 本文已影响0人
VincentH
传统的处理文件下载隐藏真实地址,相信很多小伙伴都能够在网上找到以下代码:
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($filepath));
header('Content-Transfer-Encoding: binary');
header('Expires: 0′);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0′);
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
readfile($file_path);
代码确实能下载文件,但是,当文件过大的时候,就会出现意想不到的错误:
- 下载的文件莫名变小
- 下载的文件网络错误
以下为大文件正常下载代码:
$filesize=filesize($file)+1000;
header('Content-Description:File Transfer');
header("Content-Type:application/octet-stream");
header('Content-Transfer-Encoding:binary');
header("Accept-Ranges: bytes");
header('Expires:0');
header('Cache-Control:must-revalidate');
header('Pragma:public');
header("Content-Length:".$filesize);
header("Content-Disposition:attachment;filename=".$file_naked);
$fp = fopen($file, "rb");
fseek($fp,0);
while (!feof($fp)) {
set_time_limit(0);
print (fread($fp, 1024 * 8));
flush();
ob_flush();
}
fclose($fp);
exit ();