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 .
Page 1 of 1
Uploadimage image
#2
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.
However, if you want to upload image to your server and store the path in database, this wiki may help you.
#3
Posted 23 August 2011 - 03:21 AM
u can find and use some upload and thumb extensions
My Blog:http://yxmhero1989.blog.163.com/
#4
Posted 25 December 2011 - 12:36 PM
In controller... do like this..
in model do like thiss...
i think thats the logic.. if u need complete code Email me
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
#5
Posted 28 December 2011 - 08:31 AM
nigilan post is perfetct.
Model:
View:
Control:
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']);
#6
Posted 26 November 2012 - 01:43 PM
My image is not saving in the upload folder, any help will be helpful.
Thanks in advance.
Thanks in advance.
#7
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.
Share this topic:
Page 1 of 1

Help













