I am using Uploadify to upload Files and using Codeigniter framework.
Here is my Uploadify code :
$("#change_profile_icon").uploadify({
'uploader' : '/project/style/scripts/crop/uploadify/uploadify.swf',
'script' : 'http://localhost/project/pages/profile_icon',
'cancelImg' : '/project/style/scripts/crop/uploadify/cancel.png',
'buttonText' :'Upload image',
'width' : '110',
'height' : '30',
'queueID' : 'fileQueue',
'auto' : true,
'scriptData' :{username :"<?php echo $this->session->userdata('username');?>",folder:"honda"},
'queueSizeLimit' : 1,
'multi' : false,
'fileDesc' : 'jpg',
'fileExt' : '*.jpg;*.png',
'sizeLimit' : '819200',//max size bytes - 800kb
'onComplete' : function(event,queueID,fileObj,response,data) {
alert("Completed");
var dataresponse = eval('(' + response + ')');
//$('#uploadifyUploader').remove();
var filenametmp = "http://localhost"+(dataresponse.file).substring(0,(dataresponse.file).lastIndexOf("?"));
var current_page = $('#page-list').val();
},
'onSelect' : function (){
var folder = $('#page-list option:selected').text(); //returns HONDA which is correct
$('#change_profile_icon').uploadifySettings('folder',folder);
} ,
'onError' : function(){
alert('error');
}
});
Here is my PHP part [script value in Uploadify]
function profile_icon()
{
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
// $fileTypes = str_replace('*.','',$_REQUEST['fileext']);
// $fileTypes = str_replace(';','|',$fileTypes);
// $typesArray = split('\|',$fileTypes);
// $fileParts = pathinfo($_FILES['Filedata']['name']);
// if (in_array($fileParts['extension'],$typesArray)) {
// Uncomment the following line if you want to make the directory if it doesn't exist
$targetPath = 'uploads/' .$_REQUEST['folder']. '/';
$targetFile = $targetPath.$_FILES['Filedata']['name'];
if (!file_exists($targetPath))
{
mkdir(str_replace('//','/',$targetPath), 0755, true);
}
move_uploaded_file($tempFile,$targetFile);
echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
// } else {
// echo 'Invalid file type.';
// }
}
Problem :
$targetPath = 'uploads/' .$_REQUEST['folder']. '/';
$targetFile = $targetPath.$_FILES['Filedata']['name'];
if (!file_exists($targetPath))
{
mkdir(str_replace('//','/',$targetPath), 0755, true);
}
Check the above codes taken from the PHP part.I think that $_REQUEST['folder']
will give the folder name which is specified on the Uploadify script.The value of folder
is Honda
But this gives something different.
I uploaded a file and this script uploaded it to
C:\wamp\www\project\uploads\project\home\editpage\honda\honda
On wamp server [I am in Localhost]
But how it comes ?? it should be
C:\wamp\www\project\uploads\honda
Check the below...
$targetPath = 'uploads/' .$_REQUEST['folder']. '/';
$targetFile = $targetPath.$_FILES['Filedata']['name'];
The targetPath
should be now uploads/honda/
and targetFile
should be now uploads/honda/fileName.ext
I dont know what i am doing wrong and where it is....
Please help me.
Thank you .
EDIT : THE URL STRUCTURE OF CURRENT PAGE : http://localhost/Project/home/editpage/honda/
Where home
is a controller and editpage
is a function and honda
is a argument.[Codeigniter framework]
SOLVED
I solved the issue,it is a bug in uploadify : The uploadify folder variable is not straight forward ,so that we should add a slash
before that.
so it would be var folder = "/"+ "FolderName";
The problem is u cant return the data on server if u use just Folder name.