Jquery File Upload change session id

佐手、 提交于 2019-12-13 06:12:56

问题


I've been trying to solve this problem for days without success.

I am using blueimp Jquery File Upload and everything works fine but I need to save my pictures in different folders depending on a parameter send by url like /index.php?customer=160

<input type="hidden" name="customer" value="<?php print $_GET["id_cliente"];?>">

I created the hidden field in the form and got it in the uploadhanndler.php.

/files/'.$_POST['customer'].'

, here everything goes ok, the file is saved on the folder I wanted, but the next time I open the window /index.php?customer=160, the files listed are the files/ folder not the files/160/ folder or the number I want to list.

I realized I could use the PHP user directories, and the files are beeing saved in a folder like this 6afa0f7338b14aeed39eb7656f364b4e that comes from the session_id(), I tried then to change the session_id() with the number of the folder I want this way at the begining of the /index.php?customer=160

session_start();
session_id($_GET['customer']);

but the files are still beeing saved in folder 6afa0f7338b14aeed39eb7656f364b4e, and when I print the session_id() is 160.

PHP user directories is a good method to achieve what I want? what I am doing wrong?

Thanks in advance and excuse my poor english.


回答1:


The code below will show you how to save a session ID called customer_id and then use it as a folder name. Please let me know if you need any more help.

// start (or restart) the session
session_start();

// set the customer id session var
$_SESSION['customer_id'] = // set this variable something that you've retrieved from the DB

// create a folder if it doesn't already exist
$dir = '/httpdocs/mySite/customers/' . $_SESSION['customer_id'];
if (!file_exists($dir)) {
    mkdir($dir, 0777, true);
}



回答2:


even it has been a while since a last answer has been given to this question, I'd like to give a small update on that issue. Since version 5.17 of the jquery-file-upload library ther is a support for user-directories. Within the UploadHandler.php you will find the option 'user_dirs' => false. Just set it to 'true' and you will have user-directories based on session-id.

If you want to have your own user-directories not based on session-ids, but e.g. based on user-id (your own defined Session key) you can proceed in the following way:

within the index.php file (as stated in the Demo of the jquery-file-upload library) you place following right after "require('UploadHandler.php');"

class CustomUploadHandler extends UploadHandler {
    protected function get_user_id() {
        @session_start();
        return $_SESSION[*<your own user id session key>*];
    }
}

followed by:

$upload_handler = new CustomUploadHandler(array(
    'user_dirs' => true
));

...and thats it...now you have your own user-directories for your uploaded files



来源:https://stackoverflow.com/questions/20613238/jquery-file-upload-change-session-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!