Upload Image Yii

Hello guys, i’m new to Yii and completely newbie in Yii.

after spent some times read this article http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/ so i’m trying to create 1 form which 1 of its fields will upload the image and store its image path location into database.

here are my code,

model




return array(

			array('product_price', 'numerical', 'integerOnly'=>true),

			array('product_name', 'length', 'max'=>255),

			array('product_img_path', 'file', 'types'=>'jpg, jpeg, png',),

			array('product_specification', 'safe'),

			// The following rule is used by search().

			// @todo Please remove those attributes that should not be searched.

			array('product_id, product_name, product_specification, product_price, product_img_path', 'safe', 'on'=>'search'),

		);



controller




public function actionCreate()

	{

		$model=new Product;


		// Uncomment the following line if AJAX validation is needed

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


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

		{

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

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

			if($model->save())

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

				$model->product_img_path->saveAs('E:/Web_Project/store/product_images/');

		}


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

			'model'=>$model,

		));

	}



_form file




<div class="row">

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

		<?php echo $form->fileField($model,'product_img_path'); ?>

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

	</div>



there is no error appear but image won’t uploaded and path won’t stored.

please help me where i made a wrong.

thank you.

I think it’s because the file name is not safe. Try this:


array('product_img_path', 'file', 'safe'=>true, 'types'=>'jpg, jpeg, png',)

thanks Pecos.

i’ve tried. it still won’t upload the image and won’t store path location.

help me please…

Thank you in advance.

ups sorry, i’m not follow the guide correctly. i’ve changed on yellow line.

i shouldn’t write saveAs code after redirect. but it still didn’t works.

controller




public function actionCreate()

	{

		$model=new Product;


		// Uncomment the following line if AJAX validation is needed

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


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

		{

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

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

			if($model->save())

[color="#FFFF00"]                                $model->product_img_path->saveAs('E:/Web_Project/store/product_images/');

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

[/color]		}


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

			'model'=>$model,

		));

	}



If it doesn’t get stored at all the first problem is that line. My guess is it returns false. You should check the application log (protected/runtime). It will tell you exactly what the error is. Also echo something after the save to make sure where it goes wrong.

Hello Pecos,

thanks for your time. i’ve got other usefull link to guide http://www.throttleengineer.com/blog/uploading-a-file-with-the-yii-framework/.




public function actionCreate()

	{

		$model=new Product;


		// Uncomment the following line if AJAX validation is needed

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


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

		{

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

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

			if($model->save())

				$model->product_brief_img->saveAs('./product_images/'.$model->product_brief_img->name);

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

		}


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

			'model'=>$model,

		));

	}



i’m sorry it should works and give an error, but those codes i wrote on create action and i tested on create site.

thanks Pecos! my case solved.