Blueimp jQuery fileupload Plugin and Symfony : how to dynamically change file repository name based on an id?

I use the plugin 'blueimp jquery fileupload' to upload files in Javascript (that part I got it right) and then I have to handle the uploads on the server side using an uploadhandler affiliated to the plugin (UploadHandler.php).

Working in Symfony, I managed to create a service based on this php script and it works in my controller (the files are uploaded in the default repository, yet on my page I get the error message 'upload failed' and I don't know why but it's not a big problem I guess), but the thing is :

I would like to custom the repository path to upload the files based on the user id, and since I call the uploadhandler file as a service, I don't know how to override the options using the construct function, as I would be able to with a basic call in php.

Here's my code :

public function uploadFiles(Request $request)
{
    $uploadhandler  = $this->container->get('extranetcontratbundle.uploadhandler');
    $response = $uploadhandler->response;
    $files = $response['files'];

    return new JsonResponse($files);
}

In the options of UploadHandler.php there is :

$this->options = array(
        'script_url' => $this->get_full_url().'/'.$this->basename($this->get_server_var('SCRIPT_NAME')),
        'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/files/',
        'upload_url' => $this->get_full_url().'/files/',
        'input_stream' => 'php://input',
        'user_dirs' => false,
        'mkdir_mode' => 0755,
        'param_name' => 'files', 
...blabla

And I would like to override the options in a similar way as I would in 'normal' php :

$tmpImagesDir = JPATH_ROOT . 'tmp' . $userId .;
$tmpUrl = 'tmp/' . $userId . '/' . '/';

$uploadOptions = array('upload_dir' => $tmpImagesDir, 'upload_url' => $tmpUrl);
$uploadHandler = new UploadHandler($uploadOptions);

But to do that I would have to write "require_once(blabla)" and I would have created the service for nothing. If I understood it right, that's not the way to do it in Symfony. Is there a way ?

Thank you for reading, please help.

728x90

0 Answers Blueimp jQuery fileupload Plugin and Symfony : how to dynamically change file repository name based on an id?