• 技术文章 >后端开发 >PHP问题

    php下载文件的方法【总结】

    藏色散人藏色散人2020-08-18 10:49:03原创637

    php下载文件的方法:1、从“$_GET['file']”得到文件路径;2、设置header信息;3、使用“file_get_contents()”和“file()”方法;4、通过“readfile”和“fopen”方法。

    推荐:《PHP视频教程

    PHP下载文件的方式

    1. 得到文件路径

    从$_GET['file']得到文件路径

    $path_parts = pathinfo($_GET['file']);
    $file_name  = $path_parts['basename'];
    $file_path  = '/mysecretpath/' . $file_name;

    务必使用上面这种方法得到路径,不能简单的字符串拼接得到路径

    $mypath = '/mysecretpath/' . $_GET['file'];

    如果输入的是../../,就可以访问任何路径

    2. 设置header信息

    header('Content-Description: File Transfer'); //描述页面返回的结果
    header('Content-Type: application/octet-stream'); //返回内容的类型,此处只知道是二进制流。具体返回类型可参考http://tool.oschina.net/commons
    header('Content-Disposition: attachment; filename='.basename($file));//可以让浏览器弹出下载窗口
    header('Content-Transfer-Encoding: binary');//内容编码方式,直接二进制,不要gzip压缩
    header('Expires: 0');//过期时间
    header('Cache-Control: must-revalidate');//缓存策略,强制页面不缓存,作用与no-cache相同,但更严格,强制意味更明显
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));//文件大小,在文件超过2G的时候,filesize()返回的结果可能不正确

    3. 输出文件之file_get_contents()方法

    file_get_contents()把文件内容读取到字符串,也就是要把文件读到内存中,再输出内容

    $str = file_get_contents($file);
    echo $str;

    这种方式,只要文件稍微一大,就会超过内存限制

    4. 输出文件之file()方法

    与file_get_contents()差不多,只不过是file()会把内容按行读取到数组中,也是需要占用内存

    $f = file($file);
    while(list($line, $cnt) = each($f)) {
       echo $cnt;
    }

    文件大的时候也会超出内存限制

    5. 输出文件之readfile()方法

    readfile()方法:读入一个文件并写入到输出缓冲

    这种方式可以直接输出到缓冲,不会整个文件占用内存

    前提要先清空缓冲,先要让用户看到下载文件的对话框

    while (ob_get_level()) ob_end_clean();
    //设置完header以后
    ob_clean();
    flush();  //清空缓冲区
    readfile($file);

    这种方法可以输出大文件,读取单个文件不会超出内存限制,但下面的情况除外。

    readfile()在多人读取文件的时候同样会造成PHP内存耗尽:http://stackoverflow.com/questions/6627952/why-does-readfile-exhaust-php-memory

    PHP has to read the file and it writes to the output buffer. So, for 300Mb file, no matter what the implementation you wrote (by many small segments, or by 1 big chunk) PHP has to read through 300Mb of file eventually.

    If multiple user has to download the file, there will be a problem. (In one server, hosting providers will limit memory given to each hosting user. With such limited memory, using buffer is not going to be a good idea. )

    I think using the direct link to download a file is a much better approach for big files.

    大意:PHP需要读文件,再输出到缓冲。对于一个300M的文件,PHP最终还是要读300M内存。因此在多个用户同时下载的时候,缓冲也会耗尽内存。(不对还请指正)

    例如100个用户在下载,就需要100*buffer_size大小的内存

    6. 输出文件之fopen()方法

    set_time_limit(0);
    $file = @fopen($file_path,"rb");
    while(!feof($file))
    {
        print(@fread($file, 1024*8));
        ob_flush();
        flush();
    }

    fopen()可以读入大文件,每次可以指定读取一部分的内容。在操作大文件的时候也很有用

    7. 总结

    利用PHP下载文件时,应该要注重场景。如果本身只是几个小文件被下载,那么使用PHP下载比较好;但是如果PHP要承受大量下载请求,这时下载文件就不该交给PHP做。

    对于Apache,有mod_xsendfile可以帮助完成下载任务,更简单也更快速

    以上就是php下载文件的方法【总结】的详细内容,更多请关注php中文网其它相关文章!

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    专题推荐:php
    上一篇:如何解决php不确定参数个数的问题 下一篇:ajax php如何实现三级联动
    大前端线上培训班

    相关文章推荐

    • 直击PHP使用ajax的post方式下载excel文件简单示例• php如何隐藏真实下载路径• php如何实现下载文件• php下载图片打不开怎么办

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网