Hello,
I’m new to this framework and have no much experience…
I’ve read the tutorial in the Cookbook about uploading files using a model, but I’m always getting the following error:
[b]
Fatal error: Call to a member function saveAs() on a non-object[/b]
My code in the model(Articles.php) in function rules() is
public function rules()
{ ...
array('image', 'file',
'types'=>'jpg, gif, png',
'maxSize'=>1024 * 1024 * 50, // 50MB
'tooLarge'=>'The file was larger than 50MB. Please upload a smaller file.',
'allowEmpty'=>true
),
...
);
}
In my controller(ArticlesController.php) I have
public function actionCreate()
{
$model=new Articles;
if(isset($_POST['Articles']))
{
$model->attributes=$_POST['Articles'];
$model->image=CUploadedFile::getInstance($model,'image');
if($model->save())
{
$model->image->saveAs('/images/'.$model->image);
$image = Yii::app()->image->load('/images/'.$model->image);
$image->resize(16, 11);
$image->save('/images/'.$model->image);
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('create',array(
'model'=>$model,
));
}
and in the view form (_form.php) I have
<div class="row">
<?php echo CHtml::activeLabelEx($model,'image'); ?>
<?php echo CHtml::form('','post',array('enctype'=>'multipart/form-data')); ?>
<?php echo CHtml::activeFileField($model, 'image'); ?>
<?php echo CHtml::endForm(); ?>
<?php echo CHtml::error($model,'image'); ?>
</div>
Can anyone explain to me what is wrong??
Thank you in advance!