Yii框架上传图片用法总结


Posted in PHP onMarch 28, 2016

本文实例讲述了Yii框架上传图片用法。分享给大家供大家参考,具体如下:

Yii 提供了 CUploadedFile 来上传文件,比如图片,或者文档。

官方关于这个类的介绍 :

CUploadedFile represents the information for an uploaded file.
Call getInstance to retrieve the instance of an uploaded file, and then use saveAs to save it on the server. You may also query other information about the file, including name, tempName, type, size and error.
public properties

Property Type Description Defined By
error integer Returns an error code describing the status of this file uploading. CUploadedFile
extensionName string the file extension name for name. CUploadedFile
hasError boolean whether there is an error with the uploaded file. CUploadedFile
name string the original name of the file being uploaded CUploadedFile
size integer the actual size of the uploaded file in bytes CUploadedFile
tempName string the path of the uploaded file on the server. CUploadedFile
type string the MIME-type of the uploaded file (such as "image/gif"). CUploadedFile
实现上传文件,要用到MVC三个层面。

1、 模型层面 M ,把一个字段在rules方法里设置为 file 属性。

array('url',
    'file',  //定义为file类型
    'allowEmpty'=>true,
    'types'=>'jpg,png,gif,doc,docx,pdf,xls,xlsx,zip,rar,ppt,pptx',  //上传文件的类型
    'maxSize'=>1024*1024*10,  //上传大小限制,注意不是php.ini中的上传文件大小
    'tooLarge'=>'文件大于10M,上传失败!请上传小于10M的文件!'
),

2、视图层View,这里需要用到CHtml::activeFileField 来生成选择文件的button,注意是上传文件,所以在该标单中enctype应该设置为: multupart/form-data

<?php $form=$this->beginWidget('CActiveForm', array(
<span style="white-space:pre"> </span>'id'=>'link-form',
<span style="white-space:pre"> </span>'enableAjaxValidation'=>false,
<span style="white-space:pre"> </span>'htmlOptions' => array('enctype'=>'multipart/form-data'),
)); ?>
<div class="row">
    <?php echo $form->labelEx($model,'url'); ?>
    <?php echo CHtml::activeFileField($model,'url'); ?>
    <?php echo $form->error($model,'url'); ?>
</div>

3、控制层 C

$model=new Link;
if(isset($_POST['Link']))
{
  $model->attributes=$_POST['Link'];
  if(empty($_POST['Link']['name'])){
    $model->name = $model->url;
  }
  $file = CUploadedFile::getInstance($model,'url');
  //获得一个CUploadedFile的实例
  if(is_object($file)&&get_class($file) === 'CUploadedFile'){
  // 判断实例化是否成功
    $model->url = './assets/upfile/file_'.time().'_'.rand(0,9999).'.'.$file->extensionName;  //定义文件保存的名称
  }else{
    $model->url = './assets/upfile/noPic.jpg';
    // 若果失败则应该是什么图片
  }
  if($model->save()){
    if(is_object($file)&&get_class($file) === 'CUploadedFile'){
      $file->saveAs($model->url); // 上传图片
    }
    $this->redirect(array('view','id'=>$model->lid));
  }
}

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

PHP 相关文章推荐
PHP在字符串中查找指定字符串并删除的代码
Oct 02 PHP
php笔记之:php函数range() round()和list()的使用说明
Apr 26 PHP
PHP中filter函数校验数据的方法详解
Jul 31 PHP
Symfony2在Nginx下的配置方法图文教程
Feb 04 PHP
php注册登录系统简化版
Dec 28 PHP
php数组分页实现方法
Apr 30 PHP
php面向对象值单例模式
May 03 PHP
php cookie用户登录的详解及实例代码
Jan 03 PHP
CakePHP框架Session设置方法分析
Feb 23 PHP
PHP 7安装调试工具Xdebug扩展的方法教程
Jun 17 PHP
LaravelS通过Swoole加速Laravel/Lumen详解
Mar 02 PHP
Yii框架Session与Cookie使用方法示例
Oct 14 PHP
Yii开启片段缓存的方法
Mar 28 #PHP
CI操作cookie的方法分析(基于helper类库)
Mar 28 #PHP
CI映射(加载)数据到view层的方法
Mar 28 #PHP
CI配置多数据库访问的方法
Mar 28 #PHP
浅谈PHP中其他类型转化为Bool类型
Mar 28 #PHP
CI分页类首页、尾页不显示的解决方法
Mar 28 #PHP
CodeIgniter分页类pagination使用方法示例
Mar 28 #PHP
You might like
php7安装mongoDB扩展的方法分析
2017/08/02 PHP
Laravel中服务提供者和门面模式的入门介绍
2017/11/06 PHP
学习YUI.Ext 第二天
2007/03/10 Javascript
通过Mootools 1.2来操纵HTML DOM元素
2009/09/15 Javascript
基于Jquery+Ajax+Json的高效分页实现代码
2011/10/29 Javascript
利用jquery动画特效和css打造的侧边弹出垂直导航
2014/04/04 Javascript
解决ueditor jquery javascript 取值问题
2014/12/30 Javascript
javascript实用方法总结
2015/02/06 Javascript
深入探讨javascript函数式编程
2015/10/11 Javascript
jQuery实现的选择商品飞入文本框动画效果完整实例
2016/08/10 Javascript
ES6新特性:使用export和import实现模块化详解
2017/07/31 Javascript
gulp教程_从入门到项目中快速上手使用方法
2017/09/14 Javascript
微信小程序 数据绑定及运算的简单实例
2017/09/20 Javascript
基于复选框demo(分享)
2017/09/27 Javascript
webpack下实现动态引入文件方法
2018/02/22 Javascript
node thread.sleep实现示例
2018/06/20 Javascript
Layui给switch添加响应事件的例子
2019/09/03 Javascript
countup.js实现数字动态叠加效果
2019/10/17 Javascript
Vue中使用matomo进行访问流量统计的实现
2019/11/05 Javascript
vuex存储token示例
2019/11/11 Javascript
vue或react项目生产环境去掉console.log的操作
2020/09/02 Javascript
JavaScript WeakMap使用详解
2021/02/05 Javascript
Vue实现todo应用的示例
2021/02/20 Vue.js
python网络编程学习笔记(一)
2014/06/09 Python
Python用threading实现多线程详解
2017/02/03 Python
Python内置函数 next的具体使用方法
2017/11/24 Python
Python实现自动发送邮件功能
2021/03/02 Python
Python+matplotlib实现计算两个信号的交叉谱密度实例
2018/01/08 Python
python版本的仿windows计划任务工具
2018/04/30 Python
财务会计专业毕业生自荐信
2013/10/19 职场文书
行政专员工作职责
2013/12/22 职场文书
期末自我鉴定
2014/01/23 职场文书
怎么写好自荐书
2014/03/02 职场文书
文员岗位职责范本
2014/03/08 职场文书
2015年简历自我评价范文
2015/03/11 职场文书
欢送会主持词
2015/07/01 职场文书