[YII2] 文件上传类
//测试文件上传类
public function actionCreate()
{
$model = new Lvyou();
$upload_model = new \app\models\UploadForm();
$post = Yii::$app->request->post();
if (Yii::$app->request->isPost) {
$upload_model->file = \yii\web\UploadedFile::getInstance($upload_model, 'file');
if ($upload_model->file && $upload_model->validate()) {
$filename ='./../../uploads/' . $upload_model->file->baseName . '.' . $upload_model->file->extension;
$upload_model->file->saveAs($filename);
$post['Lvyou']['file'] = $filename;
// $ext=$model->file->extension;//上传文件后缀名
// $name=$model->file->baseName;//上传文件名
}
}
// if (isset($filename)) {
// $post['Lvyou']['file'] = $filename;
// }else{
// $post['Lvyou']['file']='他是不是没上传图片呀';
// }
if ($model->load($post) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
'upload_model'=> $upload_model,
]);
}
}
//视图层
一键生成的在index
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'file'=>[
'attribute'=>'file',
'format' => ['image',['width'=>'100','height'=>'80',]],
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
在_form
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
<?= $form->field($upload_model, 'file')->fileInput() ?>
<?php ActiveForm::end(); ?>
model层 UploadForm文件
<?php
namespace app\models; use yii\base\Model;
use yii\web\UploadedFile; /**
* UploadForm is the model behind the upload form.
*/
class UploadForm extends Model
{
/**
* @var UploadedFile file attribute
*/
public $file; /**
* @return array the validation rules.
*/
public function rules()
{
return [
[['file'], 'file'],
];
}
}
[YII2] 文件上传类的更多相关文章
- ASP.NET 文件上传类 简单好用
调用: UploadFile uf = new UploadFile(); /*可选参数*/ uf.SetIsUseOldFileName(true);//是否使用原始文件名作为新文件的文件名(默认: ...
- PHP 文件上传类
FileUpload.; $]; $_newname = date(,). : To ...
- php 文件上传类 实例分享
最近在研究php上传的内容,找到一个不错的php上传类,分享下. <?php /** * 文件上传类 * class: uploadFile * edit: www.jbxue.com */ c ...
- [上传下载] C#FileUp文件上传类 (转载)
点击下载 FileUp.zip 主要功能如下 .把上传的文件转换为字节数组 .流转化为字节数组 .上传文件根据FileUpload控件上传 .把Byte流上传到指定目录并保存为文件 看下面代码吧 // ...
- ThinkPHP文件上传类
TP框架自带文件上传类使用: 类文件在ThinkPHP/Library/Think/默认在目录下 public function upload(){ $upload = new \Think\Uplo ...
- C#文件上传类,文件流,字节数组等
using System;using System.IO;using System.Web;using System.Web.UI.WebControls; namespace DotNet.Util ...
- Php文件上传类class.upload.php
简介 Class.upload.php是用于管理上传文件的php文件上传类, 它可以帮助你快速的给自己的网站集成上传文件功能.不仅如此,此分类还有一些列的处理功能,可以对上传的文件或者本地的文件进行处 ...
- 自定义MVC框架之工具类-文件上传类
截止目前已经改造了3个类: ubuntu:通过封装验证码类库一步步安装php的gd扩展 自定义MVC框架之工具类-分页类的封装 该文件上传类功能如下: 1,允许定制上传的文件类型,文件mime信息,文 ...
- php 文件上传类,功能相当齐全,留作开发中备用吧。
收藏一个经典好用的php 文件上传类,功能相当齐全,留作开发中备用吧. 好东西,大家都喜欢,才是真的好,哈哈!!! <?php /** * 文件上传类 */ class upload ...
随机推荐
- Building Applications with Force.com and VisualForce (DEV401) 中用到的Recruiting Application介绍
1.Who uses Recruiting Application. 2. Recruiting Application Object Model
- node.js初步
Node.js介绍 Node.js 诞生于2009年,Node.js采用C++语言编写而成,是 一个Javascript的运行环境.Node.js 是一个基于 Chrome V8 引擎的 JavaSc ...
- 算法修炼之路——【链表】Leetcode24 两两交换链表中的节点
题目描述 给定一单链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是简单的改变节点内部的值,而是需要实际的进行节点交换. 示例: 输入:head = [1, 2, 3, 4] 输出:hea ...
- coding++ :JS-判断当前是否是IE浏览器,并返回时IE几?
IEVersion(); function IEVersion() { var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 var is ...
- NSArray、NSDictionary
一.NSDictionary 1.1 使用自定义对象 key Dictionaries manage pairs of keys and values. A key-value pair within ...
- BurpSuit工具安装和基本使用方法
burpsuite是渗透的必备工具,使用它可以进行一些截包分析,修改包数据.暴力破解.扫描等功能,使用最多的场景应该是设置代理拦截数据包分析数据和爆破. JDK工具下载和安装(可选) 运行BurpSu ...
- 使用Spring实例化Bean的方法以及Bean取别名
一.通过构造方法实例化Bean bean中加构造方法 public class Bean1 { public Bean1() { System.out.println("Bean1构造方法. ...
- iOS开发 - 开发版+企业版无线发布一键打包
背景:项目进入快速迭代期,需要快速地交付出AdHoc版本和企业无线发布版本.每次打包都要来回切换bundle identifier和code signing,浪费很多时间. 示例项目名称名称为Test ...
- 201771030103-陈正丽 实验一 软件工程准备—<快速浏览 邹欣老师博客—读后感>
项目 内容 <软件工程> 代祖华老师博客 作业要求 邹欣老师博客 学习目标 具体目标 在大概阅读邹欣老师的博客时,发现老师写了关于很多方面的内容,有基础的也有比较深奥的,这次阅读过程中主要 ...
- PTA数据结构与算法题目集(中文) 7-41PAT排名汇总 (25 分)
PTA数据结构与算法题目集(中文) 7-41PAT排名汇总 (25 分) 7-41 PAT排名汇总 (25 分) 计算机程序设计能力考试(Programming Ability Test,简称P ...