Yii 1.1.16 Articles with pictures cannot be modified

Source: Internet
Author: User

I have an article with a picture, but when I change it I find that I can't get the picture
This is example.php is the model file

/** * The Model class for table "Example". * * The followings was the available columns in table ' example ': * @property integer $id * @property string $img * @property string $title * @property string $url * @property integer $sta Te * @property string $detail */public function rules () {//note:you should only define rules for those attrib Utes that//would receive user inputs. Return Array (' IMG, title, state ', ' required '), Array (' state ', ' numerical ', ' integeronly ' = true), Array (' title, url ', ' Length ', ' Max ' =>150), Array (' img ', ' file ', ' AllowEmpty ' =>true, ' Ty pes ' = ' bmp,jpg,png,gif '), array (' detail ', ' safe '),//The following rule is used by search (). @todo Please remove those attributes, should not being searched. Array (' ID, img, title, URL, State, detail ', ' safe ', ' on ' = ' search '),); }

This is a 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->redi                    Rect (Array (' index '));         }else{               $this->render (' Update ', array (' model ' = $model,)); }}else{$this->render (' Update ', array (' model ' =&GT                ; $model,)); }            }    }

This is view text _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 performAjaxValidation() commented in generated controller code.    // See class documentation of CActiveForm for details on this.    'enableAjaxValidation'=>false,    'htmlOptions' => array('enctype' => 'multipart/form-data'))); ?>        
  
   labelEx($model,'img'); ?>        
  
   $model->img)); ?>图片推荐大小为500x600        
  
   isNewRecord){ echo ''; }else{ ?>
img; ?>"> error($model,'img'); ?>

The problem is that I created the successful file, to edit in addition to the image of the other content, the picture is lost, he does not save the picture

This is a picture of my successful creation

I want to edit

I changed the title a bit.

And then save the results, and the problem comes up.

In the end where the problem, I just contact Yii, my yii version is 1.1.16, please fix it!!

Reply content:

I have an article with a picture, but when I change it I find that I can't get the picture.
This is example.php is the model file.

/** * The Model class for table "Example". * * The followings was the available columns in table ' example ': * @property integer $id * @property string $img * @property string $title * @property string $url * @property integer $sta Te * @property string $detail */public function rules () {//note:you should only define rules for those attrib Utes that//would receive user inputs. Return Array (' IMG, title, state ', ' required '), Array (' state ', ' numerical ', ' integeronly ' = true), Array (' title, url ', ' Length ', ' Max ' =>150), Array (' img ', ' file ', ' AllowEmpty ' =>true, ' Ty pes ' = ' bmp,jpg,png,gif '), array (' detail ', ' safe '),//The following rule is used by search (). @todo Please remove those attributes, should not being searched. Array (' ID, img, title, URL, State, detail ', ' safe ', ' on ' = ' search '),); }

This is a 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->redi                    Rect (Array (' index '));         }else{               $this->render (' Update ', array (' model ' = $model,)); }}else{$this->render (' Update ', array (' model ' =&GT                ; $model,)); }            }    }

This is view text _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 performAjaxValidation() commented in generated controller code.    // See class documentation of CActiveForm for details on this.    'enableAjaxValidation'=>false,    'htmlOptions' => array('enctype' => 'multipart/form-data'))); ?>        
  
   labelEx($model,'img'); ?>        
  
   $model->img)); ?>图片推荐大小为500x600        
  
   isNewRecord){ echo ''; }else{ ?>
img; ?>"> error($model,'img'); ?>

The problem is that I created the successful file, to edit in addition to the image of the other content, the picture is lost, he does not save the picture

This is a picture of my successful creation

I want to edit

I changed the title a bit.

And then save the results, and the problem comes up.

In the end where the problem, I just contact Yii, my yii version is 1.1.16, please fix it!!

No more than 1, but turn over the code, your problem is in the rules.

Array (' img ', ' file ', ' AllowEmpty ' =>true, ' types ' = ' bmp,jpg,png,gif '),

CFileValidatorIf the validated field does not pass, the field is set tonull

/**     * 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);        }    }
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.