dsgnze6572 2016-11-04 09:46
浏览 25
已采纳

如何在PHP中下载大文件

I use this code to download/read files from a server.

  header("Expires: 0");
  header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  header("Cache-Control: no-store, no-cache, must-revalidate");
  header("Cache-Control: post-check=0, pre-check=0", false);
  header("Pragma: no-cache");  
  header("Content-type: application/file");
  header('Content-length: '.filesize($file_to_download));
  header('Content-disposition: attachment; filename='.basename($file_to_download));
readfile($file_to_download);
   exit;

it's working fine to download files but when the file is big it's showing an error "File not found, problem file loading". Please tell me what I could change on this code for big file downloads.

  • 写回答

1条回答 默认 最新

  • dongyong6332 2016-11-04 09:50
    关注

    Try like this:

    $chunkSize = 1024 * 1024;
    $fd = fopen($file_to_download, 'rb');
    
    while (!feof($fd)) {   
        $buffer = fread($fd, $chunkSize);
        echo $buffer;
        ob_flush();
        flush();
    }
    
    fclose($fd);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题2
  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备