Hey guys,
I’m new to Yii and was playing around with the ajaxSubmitButton function in one of my forms. It seems to post the data no problems however it’s not uploading any files. Same form works fine when not using ajax. Here is my code;
View
<?php echo $this->renderPartial('_form', array('model'=>$model)); ?>
_form view
<?php echo CHtml::ajaxSubmitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
field for uploads
<?php echo CHtml::activeFileField($model,'photos_file',array('size'=>45,'maxlength'=>45)); ?>
Controller
public function actionCreate()
{
$model=new Photos;
if(isset($_POST['Photos']))
{
$model->attributes=$_POST['Photos'];
$model->photos_file=CUploadedFile::getInstance($model,'photos_file');
if($model->save())
{
$this->uploadImage($model);
//redirect
$this->redirect(array('view','id'=>$model->photos_id));
}
}
$this->render('create',array(
'model'=>$model,
));
}
//Upload image function
public function uploadImage($photo)
{
//Check if photos dir exists
if(!is_dir(Yii::app()->params['fileStorage'].'/photos'))
{
mkdir(Yii::app()->params['fileStorage'].'/photos');
}
//Check if directory exists
$dir_to_save=Yii::app()->params['fileStorage'].'photos/'.$photo->photos_id;
if(!is_dir( $dir_to_save))
{
mkdir( $dir_to_save);
}
$photo->photos_file->saveAs($dir_to_save.'/'.$photo->photos_file->name);
//create thumbnail
$image=Yii::app()->image->load($dir_to_save.'/'.$photo->photos_file->name);
$image->resize(50,50);
$image->save($dir_to_save.'/thumb_'.$photo->photos_file->name);
}
Any ideas why i can’t upload this file using ajax?
Cheers,
Snorcha