Uploadimage

Hi all,

Am New to yii,How to upload in image in database,i have table with attributes(id,image),how to store image in database by entering in create .

Do you store image as a blob field? If yes, there is a tutorial for this.

However, if you want to upload image to your server and store the path in database, this wiki may help you.

u can find and use some upload and thumb extensions :lol:

In controller… do like this…


public function actionCreate()

	{

		$model=new Images;


		// Uncomment the following line if AJAX validation is needed

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


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

		{

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

			

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

			$myfile=CUploadedFile::getInstance($model,'image_name');

            if (is_object($myfile) && get_class($myfile)==='CUploadedFile') {

            	$model->image_name=$myfile->name;

			}


			if($model->save()) {

				//$model->imagenurl->saveAs('../images/'.$model->idProducto.'_'. $model->imagenurl);

            

				if (is_object($myfile))

    	        	$myfile->saveAs(dirname(__FILE__).'/../../images/'.$model->image_name);

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

			}

		}


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

			'model'=>$model,

		));

	}




in model do like thiss…




<?php


/**

 * This is the model class for table "images".

 *

 * The followings are the available columns in table 'images':

 * @property integer $image_id

 * @property string $image_name

 * @property string $image_desc

 */

class Images extends CActiveRecord

{

	public $image_name;

	/**

	 * Returns the static model of the specified AR class.

	 * @return Images the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'images';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('image_desc,image_pwd', 'required'),

			array('image_name, image_desc', 'length', 'max'=>50),

			array('image_name', 'file', 'allowEmpty' => FALSE, 'types' => 'jpg, jpeg, gif, png'),

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

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

			array('image_id, image_name, image_desc,image_pwd', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'image_id' => 'Image Id',

			'image_name' => 'Image',

			'image_desc' => 'User Name',

			'image_pwd' => 'Password',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('image_id',$this->image_id);

		$criteria->compare('image_name',$this->image_name,true);

		$criteria->compare('image_desc',$this->image_desc,true);

		$criteria->compare('image_pwd',$this->image_pwd,true);




		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

}



i think thats the logic… if u need complete code Email me

nigilan post is perfetct.

Model:




        public function rules()

        {

                return array(

                        array('image', 'required'),

                        array('image', 'file', 'allowEmpty' => FALSE, 'types' => 'gif, jpg, png'),

                        array('id, image', 'safe', 'on'=>'search'),

                );

        }



View:




<?php

    echo CHtml::form($this->createUrl('immagini/create'),

    'post',

    array(

        'enctype' => 'multipart/form-data'

    ));

?>

<?php echo CHtml::fileField('Immagini[immagine]'); ?>

<?php echo CHtml::submitButton('Salva'); ?>

<?php echo CHtml::endForm(); ?>



Control:




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

$model->immagine->saveAs($_POST['Immagini']['immagine']);



My image is not saving in the upload folder, any help will be helpful.

Thanks in advance.

I am also unable to upload my image here. Getting the error image size too large for every image.