php是如何下载文件的

发布时间:2020-08-28 14:28:54 作者:小新
来源:亿速云 阅读:169

这篇文章主要介绍php是如何下载文件的,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

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是如何下载文件的的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. PHP下载文件
  2. php下载文件函数

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php

上一篇:Android仿淘宝详情页面viewPager滑动到最后一张图片跳转的功能

下一篇:php是如何跳转到当前页面的

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》