dsgnze6572 2016-11-04 01: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 01: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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部