减少内存消耗在PHP在处理上传PHP输入 [英] Reduce memory consumption in PHP while handling uploads by php input

查看:160
本文介绍了减少内存消耗在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 code(它的简化)来处理在服务器端上传:

I have nginx 1.0.5 + php-cgi (PHP 5.3.6) running. I need to upload ~1GB files (1-5 parallel uploads must be). I trying to create uploading of big files through ajax upload. Everything is working but PHP eating a lot of memory for each upload. I have set memory_limit = 200M, but it's working up to ~150MB size of uploaded file. If file is bigger - uploading fails. I can set memory_limit bigger and bigger, but I think it's wrong way, cause PHP can eat all memory. I use this PHP code (it's simplified) to handle uploads on server side:

$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:

/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/sites-enabled/srv.conf:

/etc/nginx/sites-enabled/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来减少内存消耗的方法吗? 谢谢你。

Anybody knows the way to reduce memory consumption by PHP? Thanks.

推荐答案

有一个黑客,大约是伪造的内容类型头,从应用程序/八位字节流将其以的multipart / form-data的。它将停止从PHP填充$ HTTP_RAW_POST_DATA。更多细节 https://github.com/valums/file-uploader/issues/61

There's a hack, which is about faking content type header, turning it from application/octet-stream to multipart/form-data. It will stop PHP from populating $HTTP_RAW_POST_DATA. More details https://github.com/valums/file-uploader/issues/61.

这篇关于减少内存消耗在PHP在处理上传PHP输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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