frocco
(Farocco)
November 16, 2010, 8:31pm
1
Hello,
How do I add file upload to the add/edit page and controller.
If a user chooses an image, I want to upload and store the image name in the database.
If they choose nothing, I want to leave the current image name in the database.
Thanks for the help.
pc131
(Skoczy)
November 16, 2010, 8:43pm
2
You may want to look at this tutorial which is about uploading image as blob into database. To satisfy your need of leaving currently uploaded file, you may try to add rule for file field as not required, anyhow I haven’t tried it.
frocco
(Farocco)
November 16, 2010, 8:55pm
3
Thank you, I will check that out.
frocco
(Farocco)
November 17, 2010, 11:43am
4
This is working great now, but I also need to save the image as a thumb.
What do I need to change?
Thank you…
public function beforeSave()
{
if($file=CUploadedFile::getInstance($this,'uploadedFile'))
{
$this->cat_image=$file->name;
//$this->file_type=$file->type;
//$this->file_size=$file->size;
//$this->file_content=file_get_contents($file->tempName);
$file->saveAs('../img/category/'.$file->name);
}
return parent::beforeSave();
}
frocco
(Farocco)
November 17, 2010, 4:03pm
5
I resolved this using phpThumb. Thought I would share.
public function beforeSave()
{
Yii::app()->thumb->setThumbsDirectory(Yii::app()->params['imageThumbPath']);
if($file=CUploadedFile::getInstance($this,'uploadedFile'))
{
$imagePath = Yii::getPathOfAlias('webroot') . Yii::app()->params['imagePath'] . $file->name;
$this->pd_image=$file->name;
//$this->file_type=$file->type;
//$this->file_size=$file->size;
//$this->file_content=file_get_contents($file->tempName);
$file->saveAs($imagePath);
Yii::app()->thumb
->load($imagePath)
->resize(75,75)
->save($file->name);
}
}