yii 1.1.16 含圖片的文章無法修改

來源:互聯網
上載者:User

我有個含圖片的文章,但是修改的時候就發現,擷取不到圖片
這是Example.php是model檔案

/** * 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'),        );    }

這是ExampleController.php檔案

    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,                    ));                }            }    }

這是view文_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'); ?>

問題就是我建立成功的這個檔案,要編輯除了圖片的別的內容時,圖片就丟失,他不儲存圖片

這是我建立成功的圖片

我要編輯

我把標題修改了一下

然後儲存結果,問題就出現了

到底哪裡出問題裡,我剛接觸yii,我的yii版本是1.1.16,請你們修正一下!!

回複內容:

我有個含圖片的文章,但是修改的時候就發現,擷取不到圖片
這是Example.php是model檔案

/** * 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'),        );    }

這是ExampleController.php檔案

    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,                    ));                }            }    }

這是view文_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'); ?>

問題就是我建立成功的這個檔案,要編輯除了圖片的別的內容時,圖片就丟失,他不儲存圖片

這是我建立成功的圖片

我要編輯

我把標題修改了一下

然後儲存結果,問題就出現了

到底哪裡出問題裡,我剛接觸yii,我的yii版本是1.1.16,請你們修正一下!!

沒用過1,但是翻了下代碼,你的問題在rules.

array('img', 'file', 'allowEmpty'=>true, 'types'=>'bmp,jpg,png,gif'),

CFileValidator 如果驗證的欄位不通過的話會把該欄位設定成 null

/**     * 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);        }    }
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.