search
Homephp教程php手册yii使用activeFileField控件实现上传文件与图片的方法,

yii使用activeFileField控件实现上传文件与图片的方法,

本文实例讲述了yii使用activeFileField控件实现上传文件与图片的方法。分享给大家供大家参考,具体如下:

yii框架提供了activeFileField控件来完成上传文件(当然也包括了上传图片)的操作,下面介绍yii的activeFileField使用方法。

1、函数原型:
复制代码 代码如下:public static string activeFileField(CModel $model, string $attribute, array $htmlOptions=array ( ))
2、调用例子:

(1)首先,设置form,这一步一 定要做,把form设置为'multipart/form-data',具体请看我的:

<&#63;php $form=$this->beginWidget('CActiveForm', array(
'id'=>'books-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('enctype'=>'multipart/form-data'),
));
&#63;>

(2) 接着,在view下的form里设置:

<div class="row">
<&#63;php echo $form->labelEx($model,'BookImg'); &#63;>
<&#63;php echo CHtml::activeFileField($model,'BookImg'); &#63;>
<&#63;php echo $form->error($model,'BookImg'); &#63;>
</div>

(3) 如果你想预览图片,那么请注意了,可以加上这么一段:

<div class="row">
<&#63;php echo '图片预览' &#63;>
<&#63;php echo '<img  src="/static/imghwm/default1.png"  data-src="http://www.XXXX.com/'.$model- alt="yii使用activeFileField控件实现上传文件与图片的方法," >BookImg.'"  class="lazy".$model->BookImg.'"   style="max-width:90%"/>'; &#63;>
</div>

(4)最后,需要在控制类里加上下面的:

if($model->save())
{
$image=CUploadedFile::getInstance($model,'BookImg');
 if (is_object($image) && get_class($image)==='CUploadedFile')
 {
 $image->saveAs("D:/aaa/aa.jpg");//路径必须真实存在,并且如果是linux系统,必须有修改权限
 }
$this->redirect(array('view','id'=>$model->BookId));
}

请注意:这里是添加的时候使用的,修改的话要有所改变。

(5)限制上传的文件必须是图片,还有限制图片大小,那么请到model层里的rules新增这么一句:

array('BookImg', 'file','allowEmpty'=>true,
'types'=>'jpg, gif, png',
'maxSize'=>1024 * 1024 * 1, // 1MB
'tooLarge'=>'The file was larger than 1MB. Please upload a smaller file.',
)

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

您可能感兴趣的文章:

  • Yii框架登录流程分析
  • Yii中CGridView实现批量删除的方法
  • Yii基于数组和对象的Model查询技巧实例详解
  • yii权限控制的方法(三种方法)
  • yii实现使用CUploadedFile上传文件的方法
  • Yii中Model(模型)的创建及使用方法
  • yii数据库的查询方法
  • Yii中实现处理前后台登录的新方法
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)