当前位置: 首页 > news >正文

YII文件上传

<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>


相关文章:

  • Linux下ssh远程连接慢解决
  • JavaScript 无符号位移运算符 三个大于号 的使用方法
  • 高项-3月30号培训作业
  • ThreadPoolExecutor运行机制
  • 循环创建按钮
  • python3-itertools模块和迭代器函数
  • Java字节码浅析(三)
  • swift学习笔记
  • Java静态代码分析工具——FindBugs插件的安装与使用
  • 好看的网站
  • 面试题解答
  • 大话队列
  • python 学习笔记2(list/directory/文件对象/模块/参数传递)
  • 干货--JMS(java消息服务)整合Spring项目案例
  • Java基础学习总结(38)——Lombok的使用和原理
  • 深入了解以太坊
  • .pyc 想到的一些问题
  • 345-反转字符串中的元音字母
  • Docker下部署自己的LNMP工作环境
  • Java比较器对数组,集合排序
  • leetcode388. Longest Absolute File Path
  • RxJS 实现摩斯密码(Morse) 【内附脑图】
  • vue2.0开发聊天程序(四) 完整体验一次Vue开发(下)
  • Vue全家桶实现一个Web App
  • 一道闭包题引发的思考
  • 与 ConTeXt MkIV 官方文档的接驳
  • AI算硅基生命吗,为什么?
  • linux 淘宝开源监控工具tsar
  • 哈罗单车融资几十亿元,蚂蚁金服与春华资本加持 ...
  • #{}和${}的区别?
  • #1015 : KMP算法
  • #我与Java虚拟机的故事#连载08:书读百遍其义自见
  • #我与Java虚拟机的故事#连载14:挑战高薪面试必看
  • (2)STL算法之元素计数
  • (42)STM32——LCD显示屏实验笔记
  • (八)Spring源码解析:Spring MVC
  • (第二周)效能测试
  • (已更新)关于Visual Studio 2019安装时VS installer无法下载文件,进度条为0,显示网络有问题的解决办法
  • (转)编辑寄语:因为爱心,所以美丽
  • .【机器学习】隐马尔可夫模型(Hidden Markov Model,HMM)
  • .mat 文件的加载与创建 矩阵变图像? ∈ Matlab 使用笔记
  • .Net 8.0 新的变化
  • .Net 代码性能 - (1)
  • .NET 反射 Reflect
  • .NET 中各种混淆(Obfuscation)的含义、原理、实际效果和不同级别的差异(使用 SmartAssembly)
  • .net专家(张羿专栏)
  • :“Failed to access IIS metabase”解决方法
  • ?php echo ?,?php echo Hello world!;?
  • [AHOI2009]中国象棋 DP,递推,组合数
  • [Android]RecyclerView添加HeaderView出现宽度问题
  • [Android]通过PhoneLookup读取所有电话号码
  • [Angular 基础] - 表单:响应式表单
  • [AutoSar]BSW_OS 01 priority ceiling protocol(PCP)
  • [C#]扩展方法
  • [CISCN2019 华北赛区 Day1 Web5]CyberPunk --不会编程的崽