lumen 支持多文件上传
1、webform (注意:name后面一定要加[]号)
<form method="post" enctype="multipart/form-data" action="http://127.0.0.1/mobile/public/api/wx/user/upload" >
First name: <input type="file" name="file[]" /><br />
First name: <input type="file" name="file[]" /><br />
Last name: <input type="text" name="token" /><br />
<input type="submit" value="提交" />
</form>
注:HTML5的文件已经支持多文件上传:
<input type="file" name="file[]" multiple/>
2、后端处理
/*上传文件到服务器*/
public function uploadFile(\Illuminate\Http\Request $request)
{ $this->validate($request,array('token'=>'required|string'));
$this->validationUser($uid,$this->authService);
if($request->hasFile('file')) {
$root = $request->server('DOCUMENT_ROOT');
$file = $request->file('file');
//return var_dump($file);
$filePath=[]; // 定义空数组用来存放图片路径
foreach ($file as $k => $v) {
// 判断图片上传中是否出错
if (!$v->isValid()) {
$this->apiReturn("上传图片出错,请重试!",1);
}
//此处防止没有多文件上传的情况
if(!empty($v)){
if ($v->getSize() / 1024 > 600)
return $this->apiReturn("请检查您上传的文件不能大于600KB", 1);
$fileName = strtolower($v->getClientOriginalName());
if (!preg_match('/\.(jpg|jpeg|png|gif)$/', $fileName))
return $this->apiReturn("您只能上传通用的图片格式", 1);
$destinationPath = '/mobile/resources/uploads/' . date('Ymd');
$fileExtendName = substr($fileName, strpos($fileName, '.'));
$realPath = $root . $destinationPath;
if (!file_exists($realPath))
mkdir($realPath);
$newFileName = uniqid() . mt_rand(1, 100000) . $fileExtendName;
$v->move($realPath, $newFileName);
$filePath[]=$destinationPath . '/' . $newFileName;
}}
return $this->apiReturn(json_encode($filePath), 0);
}
else
return $this->apiReturn('请选择文件再上传', 1);
}
原生多文件上传:
/*多文件上传*/
public function uploadImg($file_name,$dir,$format='string')
{
$file = $_FILES[$file_name];
if($file) {
$root =$_SERVER['DOCUMENT_ROOT'];
$filePath=[]; // 定义空数组用来存放图片路径
$fileNumber=count($file['name']);
for($i=0;$i<$fileNumber;$i++) { //此处防止没有多文件上传的情况
if(!empty($file['name'][$i])){
if ($file['size'][$i] / 1024 > 600)
{
return ['error'=>"请检查您上传的文件不能大于600KB"];
} $fileName = strtolower($file['name'][$i]);
if (!preg_match('/\.(jpg|jpeg|png|gif)$/', $fileName))
{
return ['error'=>'您只能上传通用的图片格式'];
} $destinationPath = $dir. date('Ymd');
$fileExtendName = substr($fileName, strpos($fileName, '.'));
$realPath = $root . $destinationPath;
if (!file_exists($realPath))
{
make_dir($realPath);
}
$newFileName = uniqid() . mt_rand(1, 100000) . $fileExtendName;
move_uploaded_file($file['tmp_name'][$i], $realPath.'/'.$newFileName);
$filePath[]=$destinationPath . '/' . $newFileName; }
}
if($format=='string')
return implode(',',$filePath);
}
else
return ['error'=>'请选择文件再上传'];
}
lumen 支持多文件上传的更多相关文章
- 强大的支持多文件上传的jQuery文件上传插件Uploadify
支持多文件上传的jQuery文件上传插件Uploadify,目前此插件有两种版本即Flash版本和HTML5版本,对于HTML5版本会比较好的支持手机浏览器,避免苹果手机Safari浏览器不支持Fla ...
- skymvc文件上传支持多文件上传
skymvc文件上传支持多文件上传 支持单文件.多文件上传 可以设定 文件大小.存储目录.文件类型 //上传的文件目录 $this->upload->uploaddir="att ...
- 基于bootstrap的上传插件fileinput实现ajax异步上传功能(支持多文件上传预览拖拽)
首先需要导入一些js和css文件 ? 1 2 3 4 5 6 <link href="__PUBLIC__/CSS/bootstrap.css" rel="exte ...
- Jquery图片上传组件,支持多文件上传
Jquery图片上传组件,支持多文件上传http://www.jq22.com/jquery-info230jQuery File Upload 是一个Jquery图片上传组件,支持多文件上传.取消. ...
- php 图片上传 并返回上传文件位置 支持多文件上传
<?php /** * Created by PhpStorm. * User: DY040 * Date: 2018/4/26 * Time: 13:23 */ echo '<pre&g ...
- ajaxfileupload多文件上传 - 修复只支持单个文件上传的bug
搜索: jquery ajaxFileUpload AjaxFileUpload同时上传多个文件 原生的AjaxFileUpload插件是不支持多文件上传的,通过修改AjaxFileUpload少量代 ...
- jquery.form.js官方插件介绍Form插件,支持Ajax,支持Ajax文件上传
jquery.form.js官方插件介绍Form插件,支持Ajax,支持Ajax文件上传 http://www.malsup.com/jquery/form/#getting-started [JQu ...
- 聊一聊jquery文件上传(支持多文件上传)
谈到文件上传,现在一般都用现成的组件可以使用.PC端的可以使用uploadify.针对微网站H5也有uploadifive.但是这组件并不能满足各种场景的需求,例如:预览 切图 放大缩小,取消之类的. ...
- jQuery.uploadify-----文件上传带进度条,支持多文件上传的插件
借鉴别人总结的uploadify:基于jquery的文件上传插件,支持ajax无刷新上传,多个文件同时上传,上传进行进度显示,控制文件上传大小,删除已上传文件. uploadify有两个版本,一个用f ...
随机推荐
- 虹软免费人脸识别SDK注册指南
成为开发者三步完成账号的基本注册与认证:STEP1:点击注册虹软AI开放平台右上角注册选项,完成注册流程.STEP2:首次使用,登录后进入开发者中心,点击账号管理完成企业或者个人认证,若未进行实名认证 ...
- ZOJ 2480 - Simplest Task in Windows
Simplest Task in Windows Time Limit: 2 Seconds Memory Limit: 65536 KB A typical windows platfor ...
- 【视频】设计模式(C++)视频讲解
设计模式(C++) 视频网址: http://www.qghkt.com/ 设计模式(C++)视频地址: https://ke.qq.com/course/318637?tuin=a508ea62 目 ...
- 本地部署JAVA SE环境
一.下载安装JDK: 下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-doc-downloads-2133158. ...
- Aspnet mvc移除WebFormViewEngine
为了提高mvc的速度,在Global.asax中移除WebFormViewEngine protected void Application_Start() { RemoveWebFormEngine ...
- Extjs renderer函数
昨天提到了改变grid中 行的背景颜色,其实还有一个更简单的方法,就是利用renderer:function(){}函数 renderer 函数是一个拦截者模式,用于改变渲染到单元格的值和样式. re ...
- background问题
1.如果是小图的背景图 background: url("@{images-dir}/homepage/our_pro_2x.png") no-repeat 0 0; backgr ...
- 15 Django REST Framework 给api添加自定义搜索条件
一.ListModelMixin源码 # 源码 class ListModelMixin(object): """ List a queryset. "&quo ...
- MacOS10.14下Eclipse安装lombok
按照网上的教程来做,会导致eclipse启动不了,这里记录一下解决方案: 一.复制lombok.jar到../Eclipse/下 二.修改eclipse.ini,在末尾追加: -Xbootclassp ...
- C语言之各个位数上的数值之和
#include<stdio.h> #include<stdlib.h> void main() { int num; ; int x,y; printf("请输入一 ...