减少PHP中的内存消耗,同时通过php输入处理上传
问题描述:
我有nginx 1.0.5 + php-cgi(PHP 5.3.6)正在运行。 我需要上传〜1GB文件(必须有1-5个并行上传文件)。 我试图通过ajax上传创建上传大文件。一切正常,但PHP为每次上传都花费大量内存。我已经设置了memory_limit = 200M,但它的工作上传文件的大小约为150MB。如果文件较大 - 上传失败。我可以设置memory_limit越来越大,但我认为这是错误的方式,因为PHP可以吃掉所有的内存。 我用这个PHP代码(它简化)来处理在服务器端上传:减少PHP中的内存消耗,同时通过php输入处理上传
$input = fopen('php://input', 'rb');
$file = fopen('/tmp/' . $_GET['file'] . microtime(), 'wb');
while (!feof($input)) {
fwrite($file, fread($input, 102400));
}
fclose($input);
fclose($file);
/etc/nginx/nginx.conf:
user www-data;
worker_processes 100;
pid /var/run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_max_body_size 2g;
# server_tokens off;
server_names_hash_max_size 2048;
server_names_hash_bucket_size 128;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
的/ etc/nginx的/启用的站点 -/SRV .conf:
server {
listen 80;
server_name srv.project.loc;
# Define root
set $fs_webroot "/home/andser/public_html/project/srv";
root $fs_webroot;
index index.php;
# robots.txt
location = /robots.txt {
alias $fs_webroot/deny.robots.txt;
}
# Domain root
location/{
if ($request_method = OPTIONS) {
add_header Access-Control-Allow-Origin "http://project.loc";
add_header Access-Control-Allow-Methods "GET, OPTIONS, POST";
add_header Access-Control-Allow-Headers "Authorization,X-Requested-With,X-File-Name,Content-Type";
#add_header Access-Control-Allow-Headers "*";
add_header Access-Control-Allow-Credentials "true";
add_header Access-Control-Max-Age "10000";
add_header Content-Length 0;
add_header Content-Type text/plain;
return 200;
}
try_files $uri $uri/ /index.php?$query_string;
}
#error_page 404 /404.htm
location ~ index.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $fs_webroot/$fastcgi_script_name;
include fastcgi_params;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param PATH_INFO $fastcgi_script_name;
add_header Pragma no-cache;
add_header Cache-Control no-cache,must-revalidate;
add_header Access-Control-Allow-Origin *;
#add_header Access-Control-Allow-Headers "Content-Type, X-Requested-With, X-File-Name";
}
}
任何人都知道如何减少PHP的内存消耗? 谢谢。
答
之前就在同一只鞋子里,这就是我在上传过程中将文件分成不同的块。
我很好的例子是使用[1]:http://www.plupload.com/index.php“pulpload”或使用Java小程序http://jupload.sourceforge.net其中也有恢复能力,当有网络问题等试图
最重要的事情是,你希望你的文件通过网络浏览器上载有从注意到在块
答
这样做有一个黑客,大约是伪造的内容类型头,它转向从application/octet-stream
到multipart/form-data
阻止你。它将阻止PHP填充$ HTTP_RAW_POST_DATA。更多详情https://github.com/valums/file-uploader/issues/61。
我不明白你的脚本真的在做什么..你有没有尝试过使用'copy'?为什么你需要使用PHP来做这件事,而不是仅仅使用文件? – 2012-02-24 11:42:34
>我不明白你的脚本真的在做什么 保存上传的文件。 >你有没有尝试过使用复制? 它如何帮助? >为什么你需要使用PHP来做这件事,而不是仅仅使用文件? 因为我需要网络上传。我不需要FTP。 – andser 2012-02-24 11:53:52
也许你可以完全跳过PHP并使用Nginx上传模块? (http://www.grid.net.ru/nginx/upload.en.html) – mobius 2012-02-24 12:11:48