标签:左右 说明 file php app 电脑 ges bin eval
项目中有个700M左右的大文件,需要下载到本地电脑上,在网上找了段代码,如下:
1 <?php 2 $realname = "xxx.pdf"; 3 $filename = "xxx.pdf"; 4 set_time_limit(0); 5 $filesize = filesize($realname); 6 header(‘Content-Description: File Transfer‘); 7 header(‘Content-Type: application/octet-stream‘); 8 header(‘Content-Transfer-Encoding: binary‘); 9 header(‘Accept-Ranges: bytes‘); 10 header(‘Expires: 0‘); 11 header(‘Cache-Control: must-revalidate‘); 12 header(‘Pragma: public‘); 13 header(‘Content-Length: ‘ . $filesize); 14 header(‘Content-Disposition: attachment; filename=‘ . $filename); 15 16 // 打开文件 17 $fp = fopen($realname, ‘rb‘); 18 // 设置指针位置 19 fseek($fp, 0); 20 21 // 开启缓冲区 22 ob_start(); 23 // 分段读取文件 24 while (!feof($fp)) { 25 $chunk_size = 1024 * 8; // 8kb 26 echo fread($fp, $chunk_size); 27 ob_flush(); // 刷新PHP缓冲区到Web服务器 28 flush(); // 刷新Web服务器缓冲区到浏览器 29 sleep(1); // 每1秒 下载 8 KB 30 } 31 // 关闭缓冲区 32 ob_end_clean(); 33 34 fclose($fp);
完美下载!但是当部署到线上时,出现了问题:
说明:测试环境为http,线上环境为https
继续google....,找到如下代码:
<?php $realname = "xxx.pdf"; $filename = "xxx.pdf"; set_time_limit(0); $filesize = filesize($realname); 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, post-check=0, pre-check=0‘); header(‘Pragma: public‘); header(‘Content-Length: ‘ . $filesize); header(‘Content-Disposition: attachment; filename=‘ . $filename); // 打开文件 $fp = fopen($realname, ‘rb‘); ob_clean(); ob_end_flush(); while (!feof($fp)) { echo fread($fp, $filesize); ob_flush(); // 刷新PHP缓冲区到Web服务器 flush(); // 刷新Web服务器缓冲区到浏览器 } fclose($fp); exit;
完美解决!
标签:左右 说明 file php app 电脑 ges bin eval
原文地址:https://www.cnblogs.com/weibofang/p/10957016.html