大文件下载通过PHP函数的ReadFile不工作 [英] Big files download through php function readfile not working

查看:584
本文介绍了大文件下载通过PHP函数的ReadFile不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一块code的,在许多服务器上运行良好。
它是用来通过ReadFile的PHP函数下载文件。

I have a piece of code that works well on many servers. It is used to download a file through the readfile php function.

但是,在一个特定的服务器不超过25MB的大文件的工作。

But in one particular server it does not work for files bigger than 25mb.

下面是code:

        $sysfile = '/var/www/html/myfile';
        if(file_exists($sysfile)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="mytitle"');
            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($sysfile));
            ob_clean();
            flush();
            readfile($sysfile);
            exit();

当我尝试下载文件小于25MB没有问题,当文件较大,下载的文件是0字节。

When I try to download a file lower than 25mb there is no problem, when the file is bigger, the file downloaded is 0 bytes.

我试着)阅读(功能,但的file_get_contents问题仍然present。

I've tried with the function read() and file_get_contents but the problem still present.

我的PHP版本是5.5.3,内存限制设置为80MB。
错误报告已开启,但没有,即使在日志文件中显示的错误。

My php version is 5.5.3, memory limit is set to 80MB. Error reporting is on but there is no error displayed even in log file.

推荐答案

下面是完整的解决方案,由于witzawitz的答案是:

Here is the complete solution thanks to the answer of witzawitz:

我需要使用ob_end_flush()函数和FREAD();

I needed to use ob_end_flush() and fread();

<?php 
$sysfile = '/var/www/html/myfile';
    if(file_exists($sysfile)) {
   header('Content-Description: File Transfer');
   header('Content-Type: application/octet-stream');
   header('Content-Disposition: attachment; filename="mytitle"');
   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($sysfile));
   ob_clean();
   ob_end_flush();
   $handle = fopen($sysfile, "rb");
   while (!feof($handle)) {
     echo fread($handle, 1000);
   }
}
?>

这篇关于大文件下载通过PHP函数的ReadFile不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆