使用PHP和kohana的Nginx文件上传进度模块

时间:2012-08-24 18:10:02

标签: php file-upload nginx mootools kohana

当我尝试上传文件时,我会在firebug中找到它:

GET http://000.000.00.00/progress?X-Progress-ID=7

({ "state" : "starting" });

它一次又一次地回来......

不知道为什么......而且文件似乎也没有上传。

这是我的nginx配置:

user  nginx nginx;
worker_processes  1;

worker_rlimit_nofile 1024;

worker_priority -1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid       logs/nginx.pid

events {
    multi_accept        on;
    worker_connections  1024;
    use                 epoll;
}

http {
    access_log  off;
    include       mime.types;
    default_type  application/octet-stream;
    upload_progress uploads 5m; #For upload

    sendfile        on;
    #tcp_nopush     on;

   # keepalive_timeout  65;
   # client_body_timeout 65;
   # client_header_timeout 65;
   # client_max_body_size 10m;

    gzip  on;
    gzip_min_length  1000;
    gzip_http_version 1.1;
    gzip_vary on;
    gzip_comp_level 6; # The compression level, between 1 and 9, where 1 is the least compression (fastest) and 9 is the most (slowest).
    gzip_proxied     expired no-cache no-store private auth;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss application/rss+xml application/xml+atom application/atom+xml application/xml+rdf application/rdf+xml text/javascript application/javascript text/x-js;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    log_not_found off;

    error_log /var/log/nginx/error.log debug;

    include /var/nginx/sites-enabled/*;
}

这是conf。在“sites-enabled”文件夹中找到:

server {
    listen  80;
    server_name     000.000.00.00; # not my real IP...

    root    /var/www;
    index   index.php;

    location / {
            try_files $uri $uri/ /index.php?$args;
    }

    location ~ /\. {
            deny  all;
    }

    location ~* \.php$ {
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            include /usr/local/nginx/conf/fastcgi_params;
    }

    location @frontcontroller {
            rewrite ^ /upload.php last;
    }

    location = /progress {
            report_uploads uploads;
    }

    location /upload {
            # Pass altered request body to this location
            upload_pass @frontcontroller;

            # Store files to this directory
            # The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
            upload_store /var/tmp/fuploads 1;

            # Allow uploaded files to be read only by user
            upload_store_access user:r group:r all:r; ### Need to change or check "user"

            # Set specified fields in request body
            upload_set_form_field $upload_field_name.name "$upload_file_name";
            upload_set_form_field $upload_field_name.path "$upload_tmp_path";

            # Inform backend about hash and size of a file
            upload_aggregate_form_field $upload_field_name.sha1 "$upload_file_sha1";
            upload_aggregate_form_field $upload_field_name.size "$upload_file_size";

            # This directive specifies any extra POST fields which should be passed along.
            #upload_pass_form_field "^usession$";

            upload_cleanup 400 404 499 500-505;

            track_uploads uploads 5s;
    }

    #error_page  404              /404.html;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
            root    html;
    }
}

我的HTML和JS:

<!DOCTYPE HtMl>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Nginx Upload</title>
<style>
.bar {
  width: 300px;
}

#progress {
  background: #eee;
  border: 1px solid #222;
  margin-top: 20px;
}

#progressbar {
  width: 0px;
  height: 24px;
  background: #333;
}
</style>
</head>
<body>
<form id="javascript-upload" action="/upload" enctype="multipart/form-data" method="post">
    <ol>
        <li><label for="jfile">File Upload: </label> <input id="jfile" name="file" type="file" /></li>
        <li><input type="submit" value="Upload File" /></li>
    </ol>
</form>
<div style="border: 1px solid black; width: 300px;">
    <div id="status" style="background-color: #D3DCE3; width: 0px; height: 12px; margin: 1px;"></div>
</div>
<div>
<span id="received"> </span>
<span id="speed"> </span>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/mootools/1.4.5/mootools-yui-compressed.js"></script>
<script>
window.addEvent('domready', function(){
document.id('javascript-upload').addEvent('submit', function(e) {

    e.preventDefault();

    var received = 0,
        percent  = 0.0,
        perform,
        periodical,
        uuid = Math.floor(Math.random() * 16).toString(16), // Unique uploader ID
        check = 2000;

    document.id('javascript-upload').set('action', '?X-Progress-ID=' + uuid); // Assign ID to upload.

    var request = new Request.JSON({
        url: '/progress?X-Progress-ID=' + uuid, // Using same identifier!
        method: 'get',
        link: 'cancel',
        onComplete: function(response) {
            var json = response;
            if (json.state == 'uploading') {
                var delta = json.received - received,
                    bytes = delta / (check / 1000),
                    received  = json.received,
                    percent   = (json.received / json.size) * 100;

                document.id('status').tween('width', 298 * percent / 100);
                document.id('received').set('html', 'Received ' + Math.round(json.received / 1024) + '/' + Math.round(json.size / 1024) + ' KB');
                document.id('speed').set('html', 'Speed ' + Math.round(bytes / 1024) + ' KB/s');

                if (percent >= 100) {
                    clearTimeout(periodical); // Upload done, stop polling Nginx.
                }
            }
        }
    });

    perform = function () {
        request.send();
    }

    periodical = perform.periodical(check);

});
});
</script>
</body>
</html>

提前感谢您的任何帮助:)

1 个答案:

答案 0 :(得分:0)

我从不使用Nginx文件上传进度模块,但这应该与Kohana完全无关。您是否尝试将noCache: true选项添加到Request初始化?