简体   繁体   中英

jquery-file-upload plugin: Can I set upload path at runtime?

I've found other posts that talk about changing the upload path for the jquery-file-upload plugin. However, the answers usually involve hardcoding it into the upload.class.php file.

I know how to do this, but the thing is, there will be multiple users uploading multiple files that need to go to different folders. I need to be able to dynamically set the destination path of the files.

Is there any way this can be done? I've gone over the jquery-file-upload documentation and there's no mention of such an option. In fact, maybe it's just me being tired, but the documentation seems to be very lacking anyway.

If there's no way to dynamically set the upload path, then one idea I have for a workaround would be to run another script after the upload is complete that will move the files from the hardcoded destination to their proper location, but it just seems silly to have to do that.

Good idea, but you don't have to hack the upload.class.php, you can override the options by passing in an array like this:

$upload_dir = '/some/other/directory/';
$options = array('upload_dir' => $upload_dir);
$upload_handler = new UploadHandler($options);

Here's the solution I finally ended up with.

First, I added URL parameters to the data-url in the input tag that the file uploader is attached to (it should be pointing to the jquery file upload server/php/ subfolder, which contains an index.php file):

<input type="file" name="files[]" data-url="/jquery-file-upload/server/php/?type=a">

I then used a SWITCH statement in the index.php file that the data-url points to:

   switch ($_GET["type"]) {

    case "a":
        $upload_dir = '/custom/folder/here/';
        $upload_url = '/custom/folder/here';
        $allowed_exts = '/\.(jpg|jpeg|gif|png)$/i';
        break;

    case "b":
        // you get the idea
        break;

    default:
        $upload_dir = dirname($_SERVER['SCRIPT_FILENAME']).'/files/';
        $allowed_exts = '/.+$/i';
        break;
}   

Finally, in the upload.class.php file I modified the appropriate lines depending on which values I wanted to customize using the SWITCH :

    function __construct($options = null, $initialize = true) {
        $this->options = array(
        'script_url' => $this->get_full_url().'/',
        'upload_dir' => $GLOBALS["upload_dir"],
        'upload_url' => $GLOBALS["upload_url"],
        'user_dirs' => false,
        'mkdir_mode' => 0755,
        'param_name' => 'files',
        'delete_type' => 'DELETE',
        'access_control_allow_origin' => '*',
        'download_via_php' => false,
        'inline_file_types' => '/\.(gif|jpe?g|png)$/i',
        'accept_file_types' => $GLOBALS["allowed_exts"],
        ...etc

The variables that I defined in the SWITCH are used here as `$GLOBALS["varname"] so that they can be used from within the class.

I hope this is able to help someone. Each person's case is going to be different so I don't recommend copying and pasting this code exactly. I was just trying to illustrate the basic idea.

NOTE: "upload_dir" is the ROOT path and "upload_url" is the public path.

Also note: As you can see in my example I also used this method to determine which filetypes to allow depending on the conditions.

There is probably a built-in way to do this through the file uploader API/Options, but the documentation is very vague about what you can actually do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM