I have small problem with uploading file in update action. In both update and create action file uploading works perfectly. Problem is, that wenn I don’t choose any file on update action, my $model->image is overriden with no value. I’d like to change it. I wish to change my controller to not update my $model->image if no file has been chosen. I alredy tried to mess with my controller, but couldn’t solve it, if anyone has an idea, please help I’ve seen in this forum some uploading solution, but neither helped
here is my controller update action:
public function actionUpdate()
{
$model=$this->loadModel();
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Company']))
{
$folder=Yii::getPathOfAlias('webroot').'/images/company/';// folder for uploaded files
$model->attributes=$_POST['Company'];
$model->logo=CUploadedFile::getInstance($model,'logo');
$model->logo=rand(99999, 10) . '_' . $file->name;
if($model->save())
{
if(!empty($model->logo))
{
if(!is_dir($folder.$model->id)){
mkdir($folder.$model->id);
}
$file->saveAs($folder . $model->id . '/'.$model->logo);
}
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('update',array(
'model'=>$model,
));
}
This will work as long as for the update action, you set the logo field as optional in the model then in the controller you will do an additional check to see if the user added an image or not (probably you will check the $_FILES)
<?php
public function actionUpdate()
{
$model=$this->loadModel();
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Company']))
{
$folder=Yii::getPathOfAlias('webroot').'/images/company/';// folder for uploaded files
$oldLogo=$model->logo;
$model->attributes=$_POST['Company'];
$model->logo=CUploadedFile::getInstance($model,'logo');
if($model->logo===null)
$model->logo=$oldLogo;
else
$model->logo=rand(99999, 10) . '_' . $file->name;
if($model->save())
{
if(!empty($model->logo))
{
if(!is_dir($folder.$model->id)){
mkdir($folder.$model->id);
}
$file->saveAs($folder . $model->id . '/'.$model->logo);
}
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('update',array(
'model'=>$model,
));
}
Thank you one more time for help, it works now, it is important to add unsafe rule for file, which I forgot
here is working example:
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id the ID of the model to be updated
*/
public function actionUpdate()
{
$model=$this->loadModel();
$folder=Yii::getPathOfAlias('webroot').'/images/ads/';// folder for uploaded files
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Ad']))
{
$model->attributes=$_POST['Ad'];
$file=CUploadedFile::getInstance($model,'image');
if(is_object($file) && get_class($file) === 'CUploadedFile')
$model->image = $file;
if($model->save())
{
if(is_object($file))
$model->image->saveAs($folder . $model->id . '/' . $model->id .'_'. $model->image);
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('update',array(
'model'=>$model,
));
}