Hamza Hamza - 6 months ago 133
Javascript Question

Change Jquery file upload url dynamically

I am using jQuery File Upload in my project. I am having problem changing the URL dynamically i.e. I want to change the upload folder dynamically. I have seen some posts e.g.

jQuery File Upload: how to change the upload url dynamically

https://github.com/blueimp/jQuery-File-Upload/issues/2640

But these both tell to change "data.url" but data.url changes the folder where PHP files are uploaded.

I am able to solve this using sessions by changing these lines in upload handler.php from

'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/files/',
'upload_url' => $this->get_full_url().'/files/',


to

'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/'.$_SESSION['cid'].'/',
'upload_url' => $this->get_full_url().'/'.$_SESSION['cid'].'/',


But what happens if user has two different pages opened at a same time then it will upload all to the same folder because session variable wont be updated. Is there any possible solution to this using Javascript? so I wont need to worry about that. Any help will be much appreciated.

Thanks

Answer

The only solution is to solve it using sessions by changing these lines in upload handler.php from

'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/files/',
'upload_url' => $this->get_full_url().'/files/',

to

'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/'.$_SESSION['cid'].'/',
'upload_url' => $this->get_full_url().'/'.$_SESSION['cid'].'/',
Comments