如何通过PHP脚本下载大文件 [英] How to download large files through PHP script

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

问题描述

使用PHP,由于授权问题,我正在尝试提供大型文件(最多可能为200MB),这些文件不在网络访问目录中。目前,我使用一个 readfile()调用以及一些标头来提供该文件,但似乎PHP在发送之前将其加载到内存中。我打算部署在一个共享托管服务器上,这不允许我使用大量的内存或添加我自己的Apache模块,如X-Sendfile。



出于安全考虑,我无法让我的文件位于网络访问目录中。有没有人知道我可以在共享托管服务器上部署的内存密度较少的方法?



编辑:

  if(/ *我的授权在这里* /){
$ path =/ uploads /;
$ name = $ row [0]; //这是一个MySQL引用,文件名为
$ fullname = $ path。 $名称; //创建文件名
$ fd = fopen($ fullname,rb);
if($ fd){
$ fsize = filesize($ fullname);
$ path_parts = pathinfo($ fullname);
$ ext = strtolower($ path_parts [extension]);
switch($ ext){
casepdf:
header(Content-type:application / pdf);
break;
casezip:
header(Content-type:application / zip);
break;
default:
header(Content-type:application / octet-stream);
break;
}
header(Content-Disposition:attachment; filename = \$ path_parts [basename]。\);
header(Content-length:$ fsize);
header(Cache-control:private); //使用它直接打开文件
while(!feof($ fd)){
$ buffer = fread($ fd,1 *(1024 * 1024));
echo $ buffer;
ob_flush();
flush(); //这两个flush命令似乎有助于执行性能
}
}
else {
echo打开文件错误;
}
fclose($ fd);


解决方案

如果您使用 fopen fread 而不是 readfile ,这应该可以解决你的问题。
$ b

解决方案 PHP的 readfile 文档显示如何使用 fread 来做你想要的。


Using PHP, I am trying to serve large files (up to possibly 200MB) which aren't in a web accessible directory due to authorization issues. Currently, I use a readfile() call along with some headers to serve the file, but it seems that PHP is loading it into memory before sending it. I intend to deploy on a shared hosting server, which won't allow me to use much memory or add my own Apache modules such as X-Sendfile.

I can't let my files be in a web accessible directory for security reasons. Does anybody know a method that is less memory intensive which I could deploy on a shared hosting server?

EDIT:

if(/* My authorization here */) {
        $path = "/uploads/";
        $name = $row[0];           //This is a MySQL reference with the filename
        $fullname = $path . $name; //Create filename
        $fd = fopen($fullname, "rb");
        if ($fd) {
            $fsize = filesize($fullname);
            $path_parts = pathinfo($fullname);
            $ext = strtolower($path_parts["extension"]);
            switch ($ext) {
                case "pdf":
                header("Content-type: application/pdf");
                break;
                case "zip":
                header("Content-type: application/zip");
                break;
                default:
                header("Content-type: application/octet-stream");
                break;
            }
            header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
            header("Content-length: $fsize");
            header("Cache-control: private"); //use this to open files directly
            while(!feof($fd)) {
                $buffer = fread($fd, 1*(1024*1024));
                echo $buffer;
                ob_flush();
                flush();    //These two flush commands seem to have helped with performance
            }
        }
        else {
            echo "Error opening file";
        }
        fclose($fd);

解决方案

If you use fopen and fread instead of readfile, that should solve your problem.

There's a solution in the PHP's readfile documentation showing how to use fread to do what you want.

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

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