2

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.

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

1 Comment

yes correct. Why bothering adding code when it's already implemented the params/options extension override? Look at the constructor of UploadHandler class: if ($options) { $this->options = array_merge($this->options, $options); }
2

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.

1 Comment

Hi thank you for your contribution. I have the same problem, i mean: i don't know how can i get ?type=a by the index.php. i would like to have a basic page that ask in which folder do you want upload the picture... when you choose which folder by click on the link... i need to say to the uploader to get the parameter so in this way in the index.php i can switch... but i cannot get the value of the parameter ?type...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.