I use Uploadify to upload files and use the Codeigniter framework.
Here is my add 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 part of PHP [uploadify script]
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 out the above PHP codes. I think $_REQUEST['folder']
will indicate the folder name specified in the Uploadify script. The folder
value is Honda
But this gives something else.
I uploaded the file and this script uploaded it to
C:\wamp\www\project\uploads\project\home\editpage\honda\honda
On the wamp server [I am in Localhost]
But how is this obtained? it should be
C:\wamp\www\project\uploads\honda
Check below ...
$targetPath = 'uploads/' .$_REQUEST['folder']. '/'; $targetFile = $targetPath.$_FILES['Filedata']['name'];
targetPath
should now be uploads/honda/
and targetFile
should now be uploads/honda/fileName.ext
I do not know what I am doing wrong, and where is it ....
Please help me.
Thanks.
EDIT : STRUCTURE OF THE CURRENT PAGE URL: http://localhost/Project/home/editpage/honda/
Where home
is the controller and editpage
is the function and Honda
is the argument. [Codeigniter framework]
SOLVED
I solved the problem, this is a mistake in uploadify: the uploadify folder variable is not direct, so we must add slash
before this.
therefore it would be var folder = "/"+ "FolderName";
The problem is that you cannot return data to the server if you use only the folder name.