目次
到底哪里出问题里,我刚接触yii,我的yii版本是1.1.16,请你们修正一下!!
回复内容:
ホームページ バックエンド開発 PHPチュートリアル yii 1.1.16 含图片的文章无法修改

yii 1.1.16 含图片的文章无法修改

Jun 06, 2016 pm 08:28 PM
php yii 改訂 写真をアップロードする

我有个含图片的文章,但是修改的时候就发现,获取不到图片
这是Example.php是model文件

<code>/**
 * 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'),
        );
    }</code>
ログイン後にコピー
ログイン後にコピー

这是ExampleController.php文件

<code>    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,
                    ));
                }
            }
    }</code>
ログイン後にコピー
ログイン後にコピー

这是view文_form.php

<code><?php $form=$this->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')
)); ?>

<div class="row">
        <?php echo $form->labelEx($model,'img'); ?>
        <?php echo CHtml::activeFileField($model,'img',array('value'=>$model->img)); ?><span class="note">图片推荐大小为500x600</span>
        <?php if($model->isNewRecord){ echo ''; }else{ ?><br><img  src="/static/imghw/default1.png" data-src="/img/bVpLOu" class="lazy"    style="max-width:90%" alt="yii 1.1.16 含图片的文章无法修改" ><?php } ?>
        <?php echo $form->error($model,'img'); ?>
    </div></code>
ログイン後にコピー
ログイン後にコピー

问题就是我创建成功的这个文件,要编辑除了图片的别的内容时,图片就丢失,他不保存图片

这是我创建成功的图片
yii 1.1.16 含图片的文章无法修改
我要编辑
yii 1.1.16 含图片的文章无法修改
我把标题修改了一下
yii 1.1.16 含图片的文章无法修改
然后保存结果,问题就出现了
yii 1.1.16 含图片的文章无法修改

到底哪里出问题里,我刚接触yii,我的yii版本是1.1.16,请你们修正一下!!

回复内容:

我有个含图片的文章,但是修改的时候就发现,获取不到图片
这是Example.php是model文件

<code>/**
 * 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'),
        );
    }</code>
ログイン後にコピー
ログイン後にコピー

这是ExampleController.php文件

<code>    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,
                    ));
                }
            }
    }</code>
ログイン後にコピー
ログイン後にコピー

这是view文_form.php

<code><?php $form=$this->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')
)); ?>

<div class="row">
        <?php echo $form->labelEx($model,'img'); ?>
        <?php echo CHtml::activeFileField($model,'img',array('value'=>$model->img)); ?><span class="note">图片推荐大小为500x600</span>
        <?php if($model->isNewRecord){ echo ''; }else{ ?><br><img  src="/static/imghw/default1.png" data-src="/img/bVpLOu" class="lazy"    style="max-width:90%" alt="yii 1.1.16 含图片的文章无法修改" ><?php } ?>
        <?php echo $form->error($model,'img'); ?>
    </div></code>
ログイン後にコピー
ログイン後にコピー

问题就是我创建成功的这个文件,要编辑除了图片的别的内容时,图片就丢失,他不保存图片

这是我创建成功的图片
yii 1.1.16 含图片的文章无法修改
我要编辑
yii 1.1.16 含图片的文章无法修改
我把标题修改了一下
yii 1.1.16 含图片的文章无法修改
然后保存结果,问题就出现了
yii 1.1.16 含图片的文章无法修改

到底哪里出问题里,我刚接触yii,我的yii版本是1.1.16,请你们修正一下!!

没用过1,但是翻了下代码,你的问题在rules.

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

CFileValidator 如果验证的字段不通过的话会把该字段设置成 null

<code>/**
     * 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);
        }
    }</code>
ログイン後にコピー
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

CakePHP プロジェクトの構成 CakePHP プロジェクトの構成 Sep 10, 2024 pm 05:25 PM

この章では、CakePHP の環境変数、一般設定、データベース設定、電子メール設定について理解します。

Ubuntu および Debian 用の PHP 8.4 インストールおよびアップグレード ガイド Ubuntu および Debian 用の PHP 8.4 インストールおよびアップグレード ガイド Dec 24, 2024 pm 04:42 PM

PHP 8.4 では、いくつかの新機能、セキュリティの改善、パフォーマンスの改善が行われ、かなりの量の機能の非推奨と削除が行われています。 このガイドでは、Ubuntu、Debian、またはその派生版に PHP 8.4 をインストールする方法、または PHP 8.4 にアップグレードする方法について説明します。

CakePHP の日付と時刻 CakePHP の日付と時刻 Sep 10, 2024 pm 05:27 PM

Cakephp4 で日付と時刻を操作するには、利用可能な FrozenTime クラスを利用します。

CakePHP ファイルのアップロード CakePHP ファイルのアップロード Sep 10, 2024 pm 05:27 PM

ファイルのアップロードを行うには、フォーム ヘルパーを使用します。ここではファイルアップロードの例を示します。

CakePHP ルーティング CakePHP ルーティング Sep 10, 2024 pm 05:25 PM

この章では、ルーティングに関連する次のトピックを学習します。

CakePHP について話し合う CakePHP について話し合う Sep 10, 2024 pm 05:28 PM

CakePHP は、PHP 用のオープンソース フレームワークです。これは、アプリケーションの開発、展開、保守をより簡単にすることを目的としています。 CakePHP は、強力かつ理解しやすい MVC のようなアーキテクチャに基づいています。モデル、ビュー、コントローラー

PHP 開発用に Visual Studio Code (VS Code) をセットアップする方法 PHP 開発用に Visual Studio Code (VS Code) をセットアップする方法 Dec 20, 2024 am 11:31 AM

Visual Studio Code (VS Code とも呼ばれる) は、すべての主要なオペレーティング システムで利用できる無料のソース コード エディター (統合開発環境 (IDE)) です。 多くのプログラミング言語の拡張機能の大規模なコレクションを備えた VS Code は、

CakePHP バリデータの作成 CakePHP バリデータの作成 Sep 10, 2024 pm 05:26 PM

Validator は、コントローラーに次の 2 行を追加することで作成できます。

See all articles