Image Upload, Saveas Issue : Image Doesn't Upload


I try uploading an image like this :

model Article :

	public $image

	public function rules()


		return array(


			array('image', 'file', 'types'=>'jpg, ,jpeg, gif, png', 'maxSize'=>1024*1024*1, 'tooLarge'=>'File has to be smaller than 1MB'),

			array('categoryId, title, description, content, url, tags, writer', 'safe', 'on'=>'search'),



View _form :

<?php $form=$this->beginWidget('CActiveForm', array(



	'htmlOptions' => array('enctype' => 'multipart/form-data'),

)); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'image'); ?>

		<?php echo CHtml::activeFileField($model, 'image'); ?>

		<?php echo $form->error($model,'image'); ?>


Controller ArticleController :

	public function actionCreate()



		$model=new Article;

		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);















The rules work correctly, the whole form too. The name of the image is correctly added to the DataBase, the path of the “saveAs” function is correct, I have no errors, but the file won’t upload to my images folder …

Thanks in advance for your help

You’re not checking the return value of


and are just immediately redirecting, so the error is being lost.

I would guess that you have a folder permissions issue. You should really wrap your logic into a transaction so that if your image saving fails, you can roll back the database insertion. Something like this:



                        $transaction = $model->dbConnection->beginTransaction();






                            if (!$model->save())

                                throw new Exception();

                            if (!$model->image->saveAs(Yii::app()->basePath.'/images/'))

                                throw new Exception();




                        catch (Exception $ex)





I’ve not included specific setting of error messages, but I’m sure you can expand on this yourself.

Thanks for your reply.

This is an interesting idea I think.

But I still have no Exception thrown and the rollback isn’t called.

My entry is still present in database but the file is not in the images folder =S

I found the error "Failed to set unsafe attribute "image" of "Article"." wich i removed, adding a safe parameter like this :

array('image', 'file', 'types'=>'jpg, ,jpeg, gif, png', 'safe'=>true, 'maxSize'=>1024*1024*1, 'tooLarge'=>'File has to be smaller than 1MB'),

But the problem remain. Everything works fine, no error, no exception, no rollback, but no file in my images folder :x

I really hope someone can help me with this I’m stuck



echo $model->image->saveAs(Yii::app()->basePath.'/images/'.$model->image->name);

returns 1 like everything works fine…



returns the following error :

move_uploaded_file(C:\wamp\www\projets\yii\science-news\protected/images/myimage.jpg) function.move-uploaded-file: failed to open stream: No such file or directory

so my path is correct too…





returns the following :


  private '_name' => string 'Clipboard01.jpg' (length=15)

  private '_tempName' => string 'C:\wamp\tmp\php72C6.tmp' (length=23)

  private '_type' => string 'image/jpeg' (length=10)

  private '_size' => int 5326

  private '_error' => int 0

  private '_e' (CComponent) => null

  private '_m' (CComponent) => null

My php.ini is well configured too :

file_uploads = On

upload_tmp_dir = "c:/wamp/tmp"

upload_max_filesize = 2M

post_max_size = 2M

max_file_uploads = 20

I really hope someone has an idea =(