Yii Framework Forum: Uploadimage - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Uploadimage image

#1 User is offline   Karthik12 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 15-March 11

Posted 09 June 2011 - 07:07 AM

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 .
0

#2 User is offline   rei 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 324
  • Joined: 10-November 10

Posted 27 July 2011 - 10:06 AM

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.
Fipick - Find and pick recommendations
0

#3 User is offline   insun 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 02-August 11

Posted 23 August 2011 - 03:21 AM

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

#4 User is offline   nigilan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 13-December 11

Posted 25 December 2011 - 12:36 PM

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
0

#5 User is offline   sensorario 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,986
  • Joined: 07-September 10
  • Location:Cesena (Italy)

Posted 28 December 2011 - 08:31 AM

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

0

#6 User is offline   sumon 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 10-November 12

Posted 26 November 2012 - 01:43 PM

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

#7 User is offline   ssmita76 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 17-April 13

Posted 17 April 2013 - 05:23 AM

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users