$_SESSION not available when using jqUploader
I am using PHP and jQuery for an uploader. I am having a huge problem though and would like some input as to how best handle these two together.
I am using a PHP session variable that I am using to change the filename after the jquery uploader has finished. The jquery "url" is pointing to a PHP function that handles the actual upload. When I use the jquery uploader to upload an image, the session variable is never available. I'm assuming because the uploader itself is not using a session.
How can I resolve this issue? I need to have that session variable to change the filename.
EDIT: I am using jqUploader
Here is the code
function jqUploadPhoto( ){
$picSize = '50550500';
$picDir = '/photos';
$temp = $_FILES['uploadfile']['name'];
... substr开发者_C百科()... convert old name to new name
$file = $_SESSION['name'].$ext; // This is set and can be echoed when this func is used from a browser. Not available in JQ
if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file) == false){ // This dies because the filename isn't available.
echo 'NOTOK';
}
}
I had this issue with Uploadify.
The problem
Your browser makes a request to your site, the session is started and a cookie containing your session ID is set.
You then make an upload using Flash, note that Flash is not your browser and it acts as a new request. So a new session id is created and a new cookie is created, giving you two different sessions.
The solution
I got around this by setting the session id in the page, eg. in a hidden input and appening this session id to the request when Flash makes the upload. You need to detect this session id when you upload and set it using session_id()
- http://php.net/manual/en/function.session-id.php . This will then replicate the session that the browser created.
Hope that helps.
精彩评论