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

在PHP中,trait适用于需要方法复用但不适合使用继承的情况。1)trait允许在类中复用方法,避免多重继承复杂性。2)使用trait时需注意方法冲突,可通过insteadof和as关键字解决。3)应避免过度使用trait,保持其单一职责,以优化性能和提高代码可维护性。

依赖注入容器(DIC)是一种管理和提供对象依赖关系的工具,用于PHP项目中。DIC的主要好处包括:1.解耦,使组件独立,代码易维护和测试;2.灵活性,易替换或修改依赖关系;3.可测试性,方便注入mock对象进行单元测试。

SplFixedArray在PHP中是一种固定大小的数组,适用于需要高性能和低内存使用量的场景。1)它在创建时需指定大小,避免动态调整带来的开销。2)基于C语言数组,直接操作内存,访问速度快。3)适合大规模数据处理和内存敏感环境,但需谨慎使用,因其大小固定。

PHP通过$\_FILES变量处理文件上传,确保安全性的方法包括:1.检查上传错误,2.验证文件类型和大小,3.防止文件覆盖,4.移动文件到永久存储位置。

JavaScript中处理空值可以使用NullCoalescingOperator(??)和NullCoalescingAssignmentOperator(??=)。1.??返回第一个非null或非undefined的操作数。2.??=将变量赋值为右操作数的值,但前提是该变量为null或undefined。这些操作符简化了代码逻辑,提高了可读性和性能。

CSP重要因为它能防范XSS攻击和限制资源加载,提升网站安全性。1.CSP是HTTP响应头的一部分,通过严格策略限制恶意行为。2.基本用法是只允许从同源加载资源。3.高级用法可设置更细粒度的策略,如允许特定域名加载脚本和样式。4.使用Content-Security-Policy-Report-Only头部可调试和优化CSP策略。

HTTP请求方法包括GET、POST、PUT和DELETE,分别用于获取、提交、更新和删除资源。1.GET方法用于获取资源,适用于读取操作。2.POST方法用于提交数据,常用于创建新资源。3.PUT方法用于更新资源,适用于完整更新。4.DELETE方法用于删除资源,适用于删除操作。

HTTPS是一种在HTTP基础上增加安全层的协议,主要通过加密数据保护用户隐私和数据安全。其工作原理包括TLS握手、证书验证和加密通信。实现HTTPS时需注意证书管理、性能影响和混合内容问题。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

Atom编辑器mac版下载
最流行的的开源编辑器

WebStorm Mac版
好用的JavaScript开发工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。