I have an article with images, but I found that the image cannot be obtained after modification. This is Example. php is the model file {code ...} this is ExampleController. PHP file {code ...} this is view File _ form. php {code ...} the problem is that the file I created is edited...
I have an article with images, but I found that the image cannot be obtained after modification.
This is Example. php. It is a model file.
/** * This is the model class for table "example". * * The followings are the available columns in table 'example': * @property integer $id * @property string $img * @property string $title * @property string $url * @property integer $state * @property string $detail */public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( array('img, title, state', 'required'), array('state', 'numerical', 'integerOnly'=>true), array('title, url', 'length', 'max'=>150), array('img', 'file', 'allowEmpty'=>true, 'types'=>'bmp,jpg,png,gif'), array('detail', 'safe'), // The following rule is used by search(). // @todo Please remove those attributes that should not be searched. array('id, img, title, url, state, detail', 'safe', 'on'=>'search'), ); }
This is the ExampleController. php file.
public function actionUpdate($id){ $model= Example::model()->findByPk($id); if($model==null){ $this->redirect(array('index')); }else{ $oldPic = $model->img; if(isset($_POST['Example'])){ $model->attributes=$_POST['Example']; $model->img = $oldPic; $fileupload = CUploadedFile::getInstance($model, 'img'); if($fileupload != null){ $filename = 'images/'.time().'.'.$fileupload->extensionName; if($fileupload->saveAs($filename)){ $model->img = $filename; if(file_exists($oldPic))unlink($oldPic); } } if($model->save()){ $this->redirect(array('index')); }else{ $this->render('update',array( 'model'=>$model, )); } }else{ $this->render('update',array( 'model'=>$model, )); } } }
This is view File _ form. php.
BeginWidget ('cactiveform', array ('id' => 'example-form', // Please note: When you enable ajax validation, make sure the corresponding // controller action is handling ajax validation correctly. // There is a call to invoke majaxvalidation () commented in generated controller code. // See class documentation of CActiveForm for details on this. 'enablesajaxvalidation '=> false, 'htmlopexception' => array ('enct Ype '=> 'multipart/form-data');?> LabelEx ($ model, 'img ');?> $ Model-> img);?> Recommended image size: 500x600 IsNewRecord) {echo '';} else {?>
Img;?> "> Error ($ model, 'img ');?>
The problem is that the file I created is lost when I want to edit content other than the image.
This is the image I created successfully.
I want to edit
I modified the title.
Then save the result. The problem occurs.
Where is the problem? I just got in touch with yii. My yii version is 1.1.16. Please correct it !!
Reply content:
I have an article with images, but I found that the image cannot be obtained after modification.
This is Example. php. It is a model file.
/** * This is the model class for table "example". * * The followings are the available columns in table 'example': * @property integer $id * @property string $img * @property string $title * @property string $url * @property integer $state * @property string $detail */public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( array('img, title, state', 'required'), array('state', 'numerical', 'integerOnly'=>true), array('title, url', 'length', 'max'=>150), array('img', 'file', 'allowEmpty'=>true, 'types'=>'bmp,jpg,png,gif'), array('detail', 'safe'), // The following rule is used by search(). // @todo Please remove those attributes that should not be searched. array('id, img, title, url, state, detail', 'safe', 'on'=>'search'), ); }
This is the ExampleController. php file.
public function actionUpdate($id){ $model= Example::model()->findByPk($id); if($model==null){ $this->redirect(array('index')); }else{ $oldPic = $model->img; if(isset($_POST['Example'])){ $model->attributes=$_POST['Example']; $model->img = $oldPic; $fileupload = CUploadedFile::getInstance($model, 'img'); if($fileupload != null){ $filename = 'images/'.time().'.'.$fileupload->extensionName; if($fileupload->saveAs($filename)){ $model->img = $filename; if(file_exists($oldPic))unlink($oldPic); } } if($model->save()){ $this->redirect(array('index')); }else{ $this->render('update',array( 'model'=>$model, )); } }else{ $this->render('update',array( 'model'=>$model, )); } } }
This is view File _ form. php.
BeginWidget ('cactiveform', array ('id' => 'example-form', // Please note: When you enable ajax validation, make sure the corresponding // controller action is handling ajax validation correctly. // There is a call to invoke majaxvalidation () commented in generated controller code. // See class documentation of CActiveForm for details on this. 'enablesajaxvalidation '=> false, 'htmlopexception' => array ('enct Ype '=> 'multipart/form-data');?> LabelEx ($ model, 'img ');?> $ Model-> img);?> Recommended image size: 500x600 IsNewRecord) {echo '';} else {?>
Img;?> "> Error ($ model, 'img ');?>
The problem is that the file I created is lost when I want to edit content other than the image.
This is the image I created successfully.
I want to edit
I modified the title.
Then save the result. The problem occurs.
Where is the problem? I just got in touch with yii. My yii version is 1.1.16. Please correct it !!
I haven't used 1, but I flipped through the Code. Your problem lies in rules.
Array ('img ', 'file', 'allowempty' => true, 'types' => 'bmp, jpg, png, gif '),
CFileValidator
If the verification field fails, the field is setnull
/** * Raises an error to inform end user about blank attribute. * Sets the owner attribute to null to prevent setting arbitrary values. * @param CModel $object the object being validated * @param string $attribute the attribute being validated */ protected function emptyAttribute($object, $attribute) { if($this->safe) $object->$attribute=null; if(!$this->allowEmpty) { $message=$this->message!==null?$this->message : Yii::t('yii','{attribute} cannot be blank.'); $this->addError($object,$attribute,$message); } }