Yii框架单文件上传
来源:互联网 发布:证券分析师 知乎 编辑:程序博客网 时间:2024/05/29 10:08
一、但图片上传
注意:本案例在yii框架高级模板的frontend代码操作
/***model层*imagsForm.php*/<?phpnamespace frontend\models;use yii\base\Model;use yii\web\UploadedFile;/*** */class InamgesForm extends Model{ // public $imageFile; // public $area; public $imageFile; public function rules() { return [ [['imageFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'], ]; } public function upload() { if ($this->validate()) { $imageName = substr(time(),4).rand(111,999); // $this->imageFile->baseName $this->imageFile->saveAs('uploads/' . $imageName . '.' . $this->imageFile->extension); return $imageName . '.' . $this->imageFile->extension; } else { return false; } }}
控制器层
/***IamgesController.php*/<?php/** * @author:木子 */namespace frontend\controllers;use Yii;use yii\web\Controller;use frontend\models\InamgesForm;use yii\web\UploadedFile;/*** */class ImagesController extends Controller{ public function actionUpload() { $model = new InamgesForm(); if(yii::$app->request->isPost) { // $allImage=UploadedFile::getInstance($model, 'imageFile'); // var_dump($allImage);die; $model->imageFile = UploadedFile::getInstance($model, 'imageFile'); if (!$image = $model->upload()) { echo "<script> alert(error::01 文件上传失败)<script>"; }else{ // 文件本地上传成功 dao入库 $dao = Yii::$app->db; $data = ['imageFile'=>$image]; $info = $dao->createCommand()->insert('images',$data)->execute(); if(!$info){ echo "<script> alert(error::02 文件入库失败!)</script>"; } echo "流程完成!";die; } } return $this->render('upload',['model'=>$model]); }}
视图层
/***iamge.php*/<?phpuse yii\widgets\ActiveForm;?><?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?> <?= $form->field($model, 'imageFile')->fileInput() ?> <button>Submit</button><?php ActiveForm::end() ?>
阅读全文
0 0
- Yii框架单文件上传
- yii 单文件上传
- yii框架文件上传
- yii框架文件上传
- yii框架中的文件上传
- Yii框架原生代码实现文件上传
- yii 各大框架文件上传(文件,视频,音乐)
- yii框架图片上传
- yii框架ajax上传
- yii 框架中的上传
- Yii框架图片上传
- yii 框架的文件上传 (简单实现)
- YII文件上传
- yii文件上传
- YII文件上传
- Yii文件上传
- yii文件上传
- yii文件上传
- Floyd-Warshall算法(最短距离)
- 搭建ELK日志分析系统(四)-kibana安装和使用
- C和指针第八章笔记
- Matplotlib 及 Seaborn 使用教程 1
- Matlab与C和C++混合编程接口及应用
- Yii框架单文件上传
- Spark的架构原理
- JDBC中增删改查
- Machine Learning---2---决策树
- 重装pycharm 软件后先前的程序无法运行
- Matplotlib 及 Seaborn 使用教程 2
- [初学笔记] 字符串
- java中一个类初始化的全过程
- 【Git】5.分支管理