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 fsockopen写的HTTP下载的类
Feb 22 PHP
php curl的深入解析
Jun 02 PHP
PHP学习笔记(二) 了解PHP的基本语法以及目录结构
Aug 04 PHP
php中get_cfg_var()和ini_get()的用法及区别
Mar 04 PHP
php如何实现只替换一次或N次
Oct 29 PHP
Zend Framework教程之Zend_Db_Table_Row用法实例分析
Mar 21 PHP
一键生成各种尺寸Icon的php脚本(实例)
Feb 08 PHP
Yii2框架实现登录、退出及自动登录功能的方法详解
Oct 24 PHP
tp5实现微信小程序多图片上传到服务器功能
Jul 16 PHP
从ThinkPHP3.2.3过渡到ThinkPHP5.0学习笔记图文详解
Apr 03 PHP
Laravel框架使用技巧之使用url()全局函数返回前一个页面的地址方法详解
Apr 06 PHP
PHP如何获取Cookie并实现模拟登录
Jul 16 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
发一个php简单的伪原创程序,配合商城采集用的
2010/10/12 PHP
浅析PHP程序设计中的MVC编程思想
2014/07/28 PHP
详解HTTP Cookie状态管理机制
2016/01/14 PHP
Yii中srbac权限扩展模块工作原理与用法分析
2016/07/14 PHP
javascript管中窥豹 形参与实参浅析
2011/12/17 Javascript
Js 获取Gridview选中行的内容操作步骤
2013/02/05 Javascript
javascrip关于继承的小例子
2013/05/10 Javascript
jquery插件jquery倒计时插件分享
2013/12/27 Javascript
node.js中的console用法总结
2014/12/15 Javascript
jQuery实现鼠标划过修改样式的方法
2015/04/14 Javascript
JavaScript创建闭包的两种方式的优劣与区别分析
2015/06/22 Javascript
Vue数据驱动模拟实现1
2017/01/11 Javascript
js实现PC端根据IP定位当前城市地理位置
2017/02/22 Javascript
Nodejs异步回调之异常处理实例分析
2018/06/22 NodeJs
vue路由事件beforeRouteLeave及组件内定时器的清除方法
2018/09/29 Javascript
layui table数据修改的回显方法
2019/09/04 Javascript
Vue2.4+新增属性.sync、$attrs、$listeners的具体使用
2020/03/08 Javascript
python在windows下实现备份程序实例
2014/07/04 Python
python复制文件的方法实例详解
2015/05/22 Python
插入排序_Python与PHP的实现版(推荐)
2017/05/11 Python
使用Python爬虫库requests发送表单数据和JSON数据
2020/01/25 Python
浅谈Pytorch torch.optim优化器个性化的使用
2020/02/20 Python
Python模拟登入的N种方式(建议收藏)
2020/05/31 Python
tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this T
2020/06/22 Python
使用Python实现音频双通道分离
2020/12/25 Python
法国最大的在线眼镜店:EasyLunettes
2019/08/26 全球购物
加拿大领先的时尚和体育零售商:Sporting Life
2019/12/15 全球购物
W Hamond官网:始于1979年的钻石专家
2020/07/20 全球购物
strlen的几种不同实现方法
2013/05/31 面试题
北京鼎普科技股份有限公司软件测试面试题
2012/04/07 面试题
大学生职业生涯规划书范文
2014/01/04 职场文书
改革共识倡议书
2014/08/29 职场文书
反邪教警示教育活动总结
2015/05/09 职场文书
国庆节主题班会
2015/08/15 职场文书
Java中Quartz高可用定时任务快速入门
2022/04/03 Java/Android
多线程Spring通过@Scheduled实现定时任务
2022/05/25 Java/Android