Image Upload, Saveas Issue : Image Doesn't Upload

Hi,

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(

	'id'=>'article-form',

	'enableAjaxValidation'=>false,

	'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'); ?>

	</div>



Controller ArticleController :




	public function actionCreate()

	{

	$this->layout='//layouts/adminColumn2';

		$model=new Article;


		// Uncomment the following line if AJAX validation is needed

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


		if(isset($_POST['Article']))

		{

			$model->attributes=$_POST['Article'];

			$model->image=CUploadedFile::getInstance($model,'image');			

			if($model->save())

			{

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

				$this->redirect(array('view','id'=>$model->id));

			}

		}


		$this->render('create',array(

			'model'=>$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


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

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:




                    if(isset($_POST['Article']))

                    {

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


                        try

                        {

                            $model->attributes=$_POST['Article'];

                            $model->image=CUploadedFile::getInstance($model,'image');

                       

                            if (!$model->save())

                                throw new Exception();


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

                                throw new Exception();


                            $transaction->commit();

                            $this->redirect(array('view','id'=>$model->id));

                        }

                        catch (Exception $ex)

                        {

                            $transaction->rollback();

                        }

                    }



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

thanks

Edit:


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

returns 1 like everything works fine…

and


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

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…


			$model->image=CUploadedFile::getInstance($model,'image');

			if($model->validate())

			{

				var_dump($model->image);die();

returns the following :


object(CUploadedFile)[65]

  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 =(