Uploading to nginx using the nginx upload module with php_handler

MK edited this page Aug 8, 2014 · 2 revisions

I have been searching a long time how to get the upload_handler from the verybasic example to work with the nginx uploadmodule. So here is my solution:

First my nginx.conf file :

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
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #    # With php5-cgi alone:
    #    fastcgi_pass;
    #    # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;

Once this is done you have to open up the verybasic.php file and look for the input field - the name of the input field will be the prefix name in our $_POST[] variable.

I call mine simple “files”:

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

Then the last step is to adapt the Uploadhandler.php.

  1. Replace the post function.

You can delete everything in the post function and replace it with something like this:

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. As you can see in the above code, we have to add two new functions:
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';
    return $s;
protected function new_generate_response($content) {
    $json = json_encode($content);
    return content;

This is it , hopefully a lot of people will enjoy it.

Thank you

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.