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学习之正则表达式
Apr 17 PHP
单一index.php实现PHP任意层级文件夹遍历(Zjmainstay原创)
Jul 31 PHP
PHP--用万网的接口实现域名查询功能
Dec 13 PHP
浅析PHP程序设计中的MVC编程思想
Jul 28 PHP
PHP中mysql_field_type()函数用法
Nov 24 PHP
php截取字符串函数分享
Feb 02 PHP
codeigniter中实现一次性加载多个view的方法
Mar 20 PHP
php简单生成随机数的方法
Jul 30 PHP
PHP使用curl制作简易百度搜索
Nov 03 PHP
解决PHP 7编译安装错误:cannot stat ‘phar.phar’: No such file or directory
Feb 25 PHP
PHP实现打包下载文件的方法示例
Oct 07 PHP
PHP通过文件路径获取文件名的实例代码
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
php获取表单中多个同名input元素的值
2014/03/20 PHP
php魔法函数与魔法常量使用介绍
2017/07/23 PHP
PHP fclose函数用法总结
2019/02/15 PHP
js word表格动态添加代码
2010/06/07 Javascript
jquery插件star-rating.js实现星级评分特效
2015/04/15 Javascript
在Ubuntu系统上安装Node.JS的教程
2015/10/15 Javascript
深入浅析NodeJs并发异步的回调处理
2015/12/21 NodeJs
JavaScript中的ParseInt(&quot;08&quot;)和“09”返回0的原因分析及解决办法
2016/05/19 Javascript
Vue之Watcher源码解析(2)
2017/07/19 Javascript
详解利用Angular实现多团队模块化SPA开发框架
2017/11/27 Javascript
vue+iview写个弹框的示例代码
2017/12/05 Javascript
微信小程序使用wxParse解析html的实现示例
2018/08/30 Javascript
微信小程序使用setData修改数组中单个对象的方法分析
2018/12/30 Javascript
vue简单练习 桌面时钟的实现代码实例
2019/09/19 Javascript
[43:32]2014 DOTA2华西杯精英邀请赛 5 25 LGD VS NewBee第一场
2014/05/26 DOTA
python回调函数用法实例分析
2015/05/09 Python
python版本的读写锁操作方法
2016/04/25 Python
Python中将dataframe转换为字典的实例
2018/04/13 Python
python3+PyQt5实现自定义分数滑块部件
2018/04/24 Python
python中的常量和变量代码详解
2018/07/25 Python
使用Python写一个量化股票提醒系统
2018/08/22 Python
详解pandas安装若干异常及解决方案总结
2019/01/10 Python
利用python将图片版PDF转文字版PDF
2019/05/03 Python
Python 转换文本编码实现解析
2019/08/27 Python
python设置环境变量的作用整理
2020/02/17 Python
Django跨域请求原理及实现代码
2020/11/14 Python
迪梵英国官方网站:Darphin英国
2017/12/06 全球购物
艺术应用与设计专业个人的自我评价
2013/11/19 职场文书
运动会解说词50字
2014/01/18 职场文书
大学生求职工作的自我评价
2014/02/13 职场文书
2014年学生资助工作总结
2014/12/18 职场文书
2015新学期家长寄语
2015/02/26 职场文书
生死抉择观后感
2015/06/09 职场文书
一年级语文教学随笔
2015/08/14 职场文书
python用字节处理文件实例讲解
2021/04/13 Python
一篇文章弄懂Python中的内建函数
2021/08/07 Python