Search code examples
phpjqueryblueimp

How to upload files to remote server using jquery blueimp


I'm working with https://github.com/blueimp/jQuery-File-Upload/ Plugin.

How can I setup this plugin so that I can upload a file to a random remote servers.

I have 5 servers with following URL's

  1. mydomain.com/files
  2. mydomain2.com/files
  3. mydomain3.com/files
  4. mydomain4.com/files
  5. mydomain5.com/files

*More server can be added dynamically when needed.

Do i need to upload the uploadhandler class (php) to each server? or what else i need to do?


Solution

  • you have to do a couple of things,

    first the random server

    server_pool = ["mydomain.com/files","mydomain2.com/files","mydomain3.com/files","mydomain4.com/files","mydomain5.com/files"];
    var theserver = server_pool[Math.floor(Math.random() * server_pool.length)];
    

    now we are going to add our random server to de main.js file inside blueimp.

    Replace this

    $('#fileupload').fileupload({
        // Uncomment the following to send cross-domain cookies:
        //xhrFields: {withCredentials: true},
        url: 'server/php/'
    });
    

    For this:

    $('#fileupload').fileupload({
        // Uncomment the following to send cross-domain cookies:
        //xhrFields: {withCredentials: true},
        // url: 'server/php/' OLD ONE
        url: theserver
    });
    

    Now is time to the PHP Part, You need to upload the server/php folder to all the other servers but first you need to add this to the index.php file inside them.

    Add this right after the

    this is the result:

    <?php
    header('Access-Control-Allow-Origin: *');
    /*
     * jQuery File Upload Plugin PHP Example 5.14
     * https://github.com/blueimp/jQuery-File-Upload
     *
     * Copyright 2010, Sebastian Tschan
     * https://blueimp.net
     *
     * Licensed under the MIT license:
     * http://www.opensource.org/licenses/MIT
     */
    
    error_reporting(E_ALL | E_STRICT);
    require('UploadHandler.php');
    $upload_handler = new UploadHandler();
    

    Cheers