<span style="font-size:14px;">use yii\web\UploadedFile;

public function actionDoartadd(){
//获取session
$session = Yii::$app->session;
$session->open();
$user_id = $session->hasSessionId;
$db = User::find()->where(['id'=>"$user_id"])->asarray()->one();
$user_name = $db['username'];
$model = new Article();
$model->attributes = $_POST;
/****图片上传start***/
$image = UploadedFile::getInstance($model, 'article_thumb');
if(!empty($image)){
$rootPath = "uploads/operate/";
$ext = $image->getExtension();
$randName = time() . rand(1000, 9999) . "." . $ext;
$image->saveAs($rootPath . $randName);
$model->article_thumb = $rootPath.$randName;
$model->user_id = $user_id;
$model->user_name = $user_name;
$model->article_addtime = time();
$rs = $model->save();
}
/****图片上传end***/
$last_id = Yii::$app->db->getLastInsertID();
$lable_id = $_POST['lable_id'];
if(count($lable_id)>3){
Yii::$app->getSession()->setFlash('error', '最多添加三个标签');
return $this->redirect('index.php?r=operate/artadd');
die; }
foreach($lable_id as $k=>$attributes)
{
$customer = new Lable();
$customer->article_id = $last_id;
$customer->artlable_id = $attributes;
$customer->save();
}
if($rs>0){
Yii::$app->getSession()->setFlash('success', '添加成功');
return $this->redirect('index.php?r=operate/index');
}else{
Yii::$app->getSession()->setFlash('error', '添加失败');
return $this->redirect('index.php?r=operate/artadd');
}
} <?= $form->field($model, 'course_thumb')->fileInput(['id' => 'change_img']) ?>
</span>

YII文件上传的更多相关文章

  1. Yii 文件上传类的使用

    https://segmentfault.com/a/1190000009674814 以上链接的文章已经写的很好了,一目了然,这里不再多做说明. 补充:当执行 UploadedFile->sa ...

  2. YII中文件上传

    文件上传 1.视图文件代码 <?php $form = $this->beginWidget("CActiveForm",array( "action&quo ...

  3. Yii2文件上传

    首先在app\controllers下建立TestController.php,内容为如下代码: <?php namespace app\controllers; use Yii; use yi ...

  4. yii2.0单文件上传和多文件上传

    yii2文件上传使用到yii2自带的文件上传类UploadFIle,以及对应的模型规则,这里分别介绍单文件上传和多文件上传: yii2单个文件上传: 上传步奏,先创建上传表单模型model(包含验证规 ...

  5. Yii2表单提交(带文件上传)

    今天写一个php的表单提交接口,除了基本的字符串数据,还带文件上传,不用说前端form标签内应该有这些属性 <form enctype="multipart/form-data&quo ...

  6. 本文档教授大家在yii2.0里实现文件上传 首先我们来实现单文件上传

    第一步  首先建立一个关于上传的model层  如果你有已经建好的可以使用表单小部件的model层 也可以直接用这个.在这里我们新建一个新的model层 在model层新建文件  Upload.php ...

  7. [YII2] 文件上传类

    //测试文件上传类 public function actionCreate() { $model = new Lvyou(); $upload_model = new \app\models\Upl ...

  8. jquery.uploadify文件上传组件

    1.jquery.uploadify简介 在ASP.NET中上传的控件有很多,比如.NET自带的FileUpload,以及SWFUpload,Uploadify等等,尤其后面两个控件的用户体验比较好, ...

  9. 11、Struts2 的文件上传和下载

    文件上传 表单准备 要想使用 HTML 表单上传一个或多个文件 须把 HTML 表单的 enctype 属性设置为 multipart/form-data 须把 HTML 表单的method 属性设置 ...

  10. Java FtpClient 实现文件上传服务

    一.Ubuntu 安装 Vsftpd 服务 1.安装 sudo apt-get install vsftpd 2.添加用户(uftp) sudo useradd -d /home/uftp -s /b ...

随机推荐

  1. Ubuntu-18.04.1-live-server-amd64.iso安装全过程

    以下向你介绍安装Ubuntu-18.04.1-live-server-amd64.iso版本的全过程,同时要说明的是Ubuntu 18.04.1 server版本只能通过文本的方式安装,即它没有和Ub ...

  2. Vue父子组件传值——第一次传不过去之“怪象”?

    前言:最近写Vue父子组件传值出现第一次传不过去之"怪象",以为Vue的BUG呢.然则,是自己太菜"^_^"!!!特此记录以警己 <spec-param& ...

  3. 02题解-洛谷 P2395 BBCode转换Markdown 题解

    洛谷 P2395 BBCode转换Markdown 题解 题目传送门: here. 一道毒瘤的大模拟,给了你一部分的 BBCode 和 Markdown 语法,叫你转换.如下表: BBCode Mar ...

  4. Lenovo Vantage 完全卸载

    Lenovo Vantage 完全卸载 我的T480,最近因为在休眠状态下断电导致不开机,按开机键所有灯闪烁.翻阅百度发现居然是通病,自从 type-c 供电方案以来就有,无奈只有被奸商坑了350,只 ...

  5. Asp.net zero项目框架和配置

  6. .net Core5.0使用IdentityServer4 进行权限控制

    .net Core5.0使用IdentityServer4 进行权限控制 IdentityServer4  ASP.NET Core的一个包含OpenID Connect和OAuth 2.0协议的框架 ...

  7. 怎样清空 DNS 缓存?

    在 Windows 下命令行执行:ipconfig /flushdns 在 macOS 下执行命令:sudo killall -HUP mDNSResponder

  8. Qt中的渲染

    Qt中3种不同的渲染方式 1 )Qt::AA_UseDesktopOpenGL 使用显卡的openGL库,且要求支持openGL 2.1及以上的版本.因此很多老旧设备是不满足版本要求的(windows ...

  9. Axure RP Extension for Chrome 0.6.2安装和详解

    1.当我们用网页访问一个本地页面时就会出现这种问题,提示你要安装浏览器的扩展程序包,以下有搜狗浏览器如何安装,qq浏览器 ​

  10. Rust for Rustaceans: Idomatic Programming for Experienced Developers Chap.2 Types

    翻译的内容如果有不理解的地方或者是其他的差错,欢迎后台回复讨论. 类型在内存中的表示 Rust中的每一个值都有自己的类型(Type).在这一章中,我们将会看到Rust中的类型服务于许多不同的目的,但其 ...