Home  >  Article  >  Backend Development  >  使用PHP下载文件的时候,如果文件过大会不会爆内存?

使用PHP下载文件的时候,如果文件过大会不会爆内存?

WBOY
WBOYOriginal
2016-06-06 20:31:491210browse

就是下载文件在PHP中,不是echo二进制的数据流,那么不是先讲二进制的数据流写入到内存,然后再输出到缓冲区。但是如果一个用户下载了一个几十g的文件,那么岂不是意味这内存就满了?

回复内容:

就是下载文件在PHP中,不是echo二进制的数据流,那么不是先讲二进制的数据流写入到内存,然后再输出到缓冲区。但是如果一个用户下载了一个几十g的文件,那么岂不是意味这内存就满了?

可以用X-Accel-Redirect把文件委托给Nginx输出:

<code><?php header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file_path).'"');
header("X-Accel-Redirect: $file_path");
</code></code>

这样就不会阻塞PHP-FPM工作进程了.
小文件Nginx会用sendfile进行处理,大文件可以用AIO线程池进行处理,基本也不会阻塞Nginx工作进程:
http://nginx.org/en/docs/http/ngx_http_core_module.html#aio

<code>location /video/ {
    sendfile on;
    aio threads;
    directio 512k;
    output_buffers 1 128k;
}
</code>

  1. 可以看先php.ini memory_limit = 128M,没有修改配置的话,单个脚本可以分配额内存不能超过128M
  2. 几十个G,这种场景绝对不存在。

php实现大文件下载,使用ob_flash以及http协议的断点续传

参考:
http://bbs.phpchina.com/thread-178853-1-1.html
http://liqwei.com/network/protocol/2011/886.shtml
http://www.cnblogs.com/thinksasa/archive/2013/02/27/2934953.html

太大的话用basket。好点的云平台都有这个吧。而是速度超级快的。小的话无所谓。反正我现在文件全部扔basket里面了。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn