结合 php_handler 的 nginx 上传模块配置


我已经找了很长一段时间关于如何从 verybasic 示例中实现一个上传处理程序(upload_handler)去结合 nginx 的上传模块。然后下面的便是我的解决方案:

首先是我的 nginx.conf 文件:

server {
    client_max_body_size 4G;
    client_body_buffer_size 1024k;
    listen 80;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm index.php;

    # Make site accessible from http://localhost/
    server_name localhost;

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

    location = /progress {
        report_uploads uploads;
    }

    location /upload {
        # the location of the php handler
        upload_pass  /jQueryFileUpload/server/php/index.php;
        # the location where the files are uploaded
        upload_store	/var/tmp/fuploads;
        upload_store_access user:rw;
        upload_state_store /var/tmp/fuploads/state ;

        # Set specified fields in request body 
        # the field data will be available through the superglobal $_POST[]
        upload_set_form_field "${upload_field_name}_name" $upload_file_name;
        upload_set_form_field "${upload_field_name}_content_type" $upload_content_type;
        upload_set_form_field "${upload_field_name}_path" $upload_tmp_path;

        upload_aggregate_form_field "${upload_field_name}_md5" $upload_file_md5;
        upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size;

        upload_pass_form_field "^submit$|^description$";

        track_uploads uploads 5s;
    }

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /index.html;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #
    #    # With php5-cgi alone:
    #    fastcgi_pass 127.0.0.1:9000;
    #    # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

在这一步完成后,你要打开 verybasic.php 文件,在里面找到 input 框,input 字段的 name 属性要对应的上我们 $_POST[] 输入的参数名

我的则只是简单叫作“files”:

<input id="fileupload" type="file" name="files" data-url="/upload" >

然后最后一步就是适配 Uploadhandler.php

  1. 替换 post 函数。

你可以直接删掉 post 函数,然后用一段类似于下面这样的代码替代:

public function post($print_response = true) {
    $prefix = files; //our name of the input field 

    if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
        return $this->delete($print_response);
    }

    $files = array();
    $files = $this->new_handle_file_upload($prefix);

    return $this->new_generate_response(array('files' => $files));
}
  1. 正如上面的代码所示,我们还需要新增两个相关函数:
protected function new_handle_file_upload($prefix) {
    $file = new stdClass();
    $file->name = $_POST[$prefix."_name"];
    $file->type = $_POST[$prefix."_content_type"];		
    $file->url = $_POST[$prefix."_path"];
    $file->size = $_POST[$prefix."_size"];
    $file->error = 'success';
    $this->set_additional_file_properties($file);
    $s[]=$file;		
    return $s;
}
protected function new_generate_response($content) {
    $this->head();
    $json = json_encode($content);
    $this->body($json);
    return content;
}

就是这样,希望大家喜欢。

谢谢!