Reputation: 4462
I need to use a session variable for username to specify the upload path in uploadify (version 3.1). In uploadifive.php using the following works fine:
$uploadDir = 'media/' . $_SESSION["user_name"] . '/';
However in uploadify, when I use the same code, files are just uploaded to the media folder (ignoring the user folder). I have echoed $_SESSION["user_name"] in uploadify.php and retrieved this using the onUploadSuccess function, and indeed the username variable is not getting passed to uploadify.php, despite starting the session.
It seems strange that this would work with uploadifive and not uploadify. I am not too savvy with PHP and would be glad of some help with this.
I include the full uploadify.php script below.
Thanks,
Nick
<?php
session_start();
/*
Uploadify
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
Released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
*/
// Define a destination
$targetPath = 'media/' . $_SESSION["user_name"] . '/';
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetFile = $targetPath . $_FILES['Filedata']['name'];
// Validate the file type
$fileTypes = array('jpg','jpeg','gif','png'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']);
if (in_array($fileParts['extension'],$fileTypes)) {
move_uploaded_file($tempFile,$targetFile);
echo '1';
} else {
echo 'Invalid file type.';
}
}
?>
Upvotes: 3
Views: 980
Reputation: 567
I've also stumbled upon this problem.
The Uploadify flash file is the one calling the PHP script, and not the browser, this means that the PHP Session isn't propagated from the browser to flash and to the the server, you only have to pass the session id by $_GET/$_POST to make the problem go away.
In the HTML file:
<input type="hidden" id="sessionid" value="<?php echo session_id(); ?>" />
In the javascript file:
$('#image').uploadify({
'uploader' : '/uploadify-v2.1.4/uploadify.swf',
'script' : '/uploadify.php',
'cancelImg' : '/uploadify-v2.1.4/cancel.png',
'folder' : '/tempfiles',
'auto' : true,
'multi' : false,
'scriptData': { 'session': $('#sessionid').val() }
});
In the PHP file:
if ( isset($_REQUEST['session']) && !empty($_REQUEST['session']) ) {
session_id($_REQUEST['session']);
}
session_start();
There seems to be a problem when the session configuration is set to auto_start ( but I haven't fully tested this ).
And this is also mentioned in the documentation of Uploadify.
Upvotes: 2
Reputation: 268364
I would check your login script to make sure that you are indeed storing the username in the session array upon authenticating the user. You're calling session_start()
, which eliminates that possibility, so if you're calling $_SESSION["user_name"]
and not getting a value, it can only mean the value isn't there to begin with, or that you got the key wrong.
Upvotes: 1