problem with upload images in yii

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!

the parameter for $model->image->saveAs is the name of file

try put




                                    $model->image->saveAs('/images/'.$model->image->name);



and use




if ( $model->image!=null  and !$model->image->getHasError() ) 



I tried the first one and I’m getting the same error.

When I put the if condition there is no error but doesn’t save anything at the image attribute so it’s probably getting a null input in the image field…

I wipped out the allowEmpty rule and I get the message "Image cannot be blank", although I have chosen the image.

Can anyone help?

hello,

Are you using window$ or *nix based OS? - if *nix, make sure that your apache (or whatever you are using) has the right permissions to that folder.

Also, maybe you can check if there is extra information about the errors in the /protected/runtime/application.log file

–iM