(updated)
Hi all.
Like many, I’m new to Yii.
I’ve been trying to install the SWFUploader (as instructed here YIISITEBASEURL/extension/swfupload). I can upload files but it is strange because can ONLY upload files to the base of my application (i.e. mysite.com/aFile.png) but I cannot upload files to directories there or in protected.
Eventually I wish to be able to upload files to a specific folder in my home directory outside of public_html (i.e. parent directory of mysite.com), or at least within a safe directory to which nobody can get access to.
Hope someone here can shed some light on what I’m doing wrong. Below I outline the steps I’m taking:
0) I created an empty folder called ‘uploadedfiles’ (755) at the base of my application (i.e. mysite.com)
1) I placed the release file’s (swfupload) folder in ‘mysite.com/protected/extensions/swfupload’
2) I placed the release file’s (handlers.js) file in ‘mysite.com/js/handlers.js’
4) I placed a controller (UploadsController.php) at ‘mysite.com/protected/controllers/UploadsController.php’
<?php
class UploadsController extends Controller
{
public function filters()
{
return array(
'accessControl -upload',
);
}
public function actionUploader(){
// In case you wish to post a variable each time a file
// is uploaded. This is good if a picture is related to an object
// and you wish to pass the ID so to related both picture
// or file to parent.
// $yourvar = 'variabletopost';
// $this->render('uploader',array('yourvar'=>$yourvar));
$this->render('uploader');
// In my case is AJAX
Yii::app()->end();
}
public function actionUpload(){
try{
$picture_file = CUploadedFile::getInstanceByName('Filedata');
if(!$picture_file || $picture_file->getHasError()){
echo 'Error: Invalid File';
Yii::app()->end();
}
$picture_name = $picture_file->name;
$destination = Yii::getPathOfAlias('uploadedfiles').$picture_name;
$picture_file->saveAs($destination);
}catch(Exception $e){
echo 'Error: ' . $e->getMessage();
}
Yii::app()->end();
}
}
?>
<?php
$filedata=$_FILES['Filedata'];
@move_uploaded_file($filedata['tmp_name'],'mysite.com/uploadedfiles/');
?>
5) I placed the following file (upload.php) at ‘mysite.com/protected/views/site/pages/’
<?php
$uurl = $this->createUrl('uploads/upload');
$this->widget('application.extensions.swfupload.CSwfUpload', array(
'jsHandlerUrl'=>'js/handlers.js', //Relative path
'postParams'=>array(),
'config'=>array(
'use_query_string'=>true,
'upload_url'=>$uurl,
'file_size_limit'=>'2 MB',
'file_types'=>'*.jpg;*.png;*.gif',
'file_types_description'=>'Image Files',
'file_upload_limit'=>0,
'file_queue_error_handler'=>'js:fileQueueError',
'file_dialog_complete_handler'=>'js:fileDialogComplete',
'upload_progress_handler'=>'js:uploadProgress',
'upload_error_handler'=>'js:uploadError',
'upload_success_handler'=>'js:uploadSuccess',
'upload_complete_handler'=>'js:uploadComplete',
'custom_settings'=>array('upload_target'=>'divFileProgressContainer'),
'button_placeholder_id'=>'swfupload',
'button_width'=>170,
'button_height'=>20,
'button_text'=>'<span class="button">'.Yii::t('messageFile', 'ButtonLabel').' (Max 2 MB)</span>',
'button_text_style'=>'.button { font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif; font-size: 11pt; text-align: center; }',
'button_text_top_padding'=>0,
'button_text_left_padding'=>0,
'button_window_mode'=>'js:SWFUpload.WINDOW_MODE.TRANSPARENT',
'button_cursor'=>'js:SWFUpload.CURSOR.HAND',
),
)
);
?>
<?php echo CHtml::beginForm(); ?>
<div class="form">
<div class="row">
<div id="divFileProgressContainer"></div>
<div class="swfupload"><span id="swfupload"></span></div>
</div>
</div>
<?php echo CHtml::endForm(); ?>
Notes: So, when I go to mysite.com/index.php?r=site/page&view=upload I’m able to see the upload form, I’m able to initiate an upload, I’m able to see the status message “Uploading…”, and then “All Images received”. But upon checking the uploadedfiles folder reveals NO UPLOADS…Is there something I’m missing?
UPDATE:
Fixed by changing the following:
$destination = Yii::getPathOfAlias(‘uploadedfiles’).$picture_name;
to:
$destination = ‘uploadedfiles/’.$picture_name;