热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

yii1.1.16含图片的文章无法修改

我有个含图片的文章,但是修改的时候就发现,获取不到图片这是Example.php是model文件{代码...}这是ExampleController.php文件{代码...}这是view文_form.php{代码...}问题就是我创建成功的这个文件,要编辑...

我有个含图片的文章,但是修改的时候就发现,获取不到图片
这是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{ ?>
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{ ?>
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);
        }
    }

推荐阅读
  • 本文介绍了如何使用jQuery和AJAX来实现动态更新两个div的方法。通过调用PHP文件并返回JSON字符串,可以将不同的文本分别插入到两个div中,从而实现页面的动态更新。 ... [详细]
  • 本文介绍了前端人员必须知道的三个问题,即前端都做哪些事、前端都需要哪些技术,以及前端的发展阶段。初级阶段包括HTML、CSS、JavaScript和jQuery的基础知识。进阶阶段涵盖了面向对象编程、响应式设计、Ajax、HTML5等新兴技术。高级阶段包括架构基础、模块化开发、预编译和前沿规范等内容。此外,还介绍了一些后端服务,如Node.js。 ... [详细]
  • 本文介绍了DataTables插件的官方网站以及其基本特点和使用方法,包括分页处理、数据过滤、数据排序、数据类型检测、列宽度自动适应、CSS定制样式、隐藏列等功能。同时还介绍了其易用性、可扩展性和灵活性,以及国际化和动态创建表格的功能。此外,还提供了参数初始化和延迟加载的示例代码。 ... [详细]
  • Allegro总结:1.防焊层(SolderMask):又称绿油层,PCB非布线层,用于制成丝网印板,将不需要焊接的地方涂上防焊剂.在防焊层上预留的焊盘大小要比实际的焊盘大一些,其差值一般 ... [详细]
  • Monkey《大话移动——Android与iOS应用测试指南》的预购信息发布啦!
    Monkey《大话移动——Android与iOS应用测试指南》的预购信息已经发布,可以在京东和当当网进行预购。感谢几位大牛给出的书评,并呼吁大家的支持。明天京东的链接也将发布。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • GetWindowLong函数
    今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • Windows下配置PHP5.6的方法及注意事项
    本文介绍了在Windows系统下配置PHP5.6的步骤及注意事项,包括下载PHP5.6、解压并配置IIS、添加模块映射、测试等。同时提供了一些常见问题的解决方法,如下载缺失的msvcr110.dll文件等。通过本文的指导,读者可以轻松地在Windows系统下配置PHP5.6,并解决一些常见的配置问题。 ... [详细]
  • 本文介绍了一种处理AJAX操作授权过期的全局方式,以解决Asp.net MVC中Session过期异常的问题。同时还介绍了基于WebImage的图片上传工具类。详细内容请参考链接:https://www.cnblogs.com/starluck/p/8284949.html ... [详细]
  • 从零基础到精通的前台学习路线
    随着互联网的发展,前台开发工程师成为市场上非常抢手的人才。本文介绍了从零基础到精通前台开发的学习路线,包括学习HTML、CSS、JavaScript等基础知识和常用工具的使用。通过循序渐进的学习,可以掌握前台开发的基本技能,并有能力找到一份月薪8000以上的工作。 ... [详细]
  • 本文介绍了Java后台Jsonp处理方法及其应用场景。首先解释了Jsonp是一个非官方的协议,它允许在服务器端通过Script tags返回至客户端,并通过javascript callback的形式实现跨域访问。然后介绍了JSON系统开发方法,它是一种面向数据结构的分析和设计方法,以活动为中心,将一连串的活动顺序组合成一个完整的工作进程。接着给出了一个客户端示例代码,使用了jQuery的ajax方法请求一个Jsonp数据。 ... [详细]
  • Vue基础一、什么是Vue1.1概念Vue(读音vjuː,类似于view)是一套用于构建用户界面的渐进式JavaScript框架,与其它大型框架不 ... [详细]
  • 工作经验谈之-让百度地图API调用数据库内容 及详解
    这段时间,所在项目中要用到的一个模块,就是让数据库中的内容在百度地图上展现出来,如经纬度。主要实现以下几点功能:1.读取数据库中的经纬度值在百度上标注出来。2.点击标注弹出对应信息。3 ... [详细]
author-avatar
mobiledu2502891487
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有