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 支持多文件上传的更多相关文章

  1. 强大的支持多文件上传的jQuery文件上传插件Uploadify

    支持多文件上传的jQuery文件上传插件Uploadify,目前此插件有两种版本即Flash版本和HTML5版本,对于HTML5版本会比较好的支持手机浏览器,避免苹果手机Safari浏览器不支持Fla ...

  2. skymvc文件上传支持多文件上传

    skymvc文件上传支持多文件上传 支持单文件.多文件上传 可以设定 文件大小.存储目录.文件类型 //上传的文件目录 $this->upload->uploaddir="att ...

  3. 基于bootstrap的上传插件fileinput实现ajax异步上传功能(支持多文件上传预览拖拽)

    首先需要导入一些js和css文件 ? 1 2 3 4 5 6 <link href="__PUBLIC__/CSS/bootstrap.css" rel="exte ...

  4. Jquery图片上传组件,支持多文件上传

    Jquery图片上传组件,支持多文件上传http://www.jq22.com/jquery-info230jQuery File Upload 是一个Jquery图片上传组件,支持多文件上传.取消. ...

  5. php 图片上传 并返回上传文件位置 支持多文件上传

    <?php /** * Created by PhpStorm. * User: DY040 * Date: 2018/4/26 * Time: 13:23 */ echo '<pre&g ...

  6. ajaxfileupload多文件上传 - 修复只支持单个文件上传的bug

    搜索: jquery ajaxFileUpload AjaxFileUpload同时上传多个文件 原生的AjaxFileUpload插件是不支持多文件上传的,通过修改AjaxFileUpload少量代 ...

  7. jquery.form.js官方插件介绍Form插件,支持Ajax,支持Ajax文件上传

    jquery.form.js官方插件介绍Form插件,支持Ajax,支持Ajax文件上传 http://www.malsup.com/jquery/form/#getting-started [JQu ...

  8. 聊一聊jquery文件上传(支持多文件上传)

    谈到文件上传,现在一般都用现成的组件可以使用.PC端的可以使用uploadify.针对微网站H5也有uploadifive.但是这组件并不能满足各种场景的需求,例如:预览 切图 放大缩小,取消之类的. ...

  9. jQuery.uploadify-----文件上传带进度条,支持多文件上传的插件

    借鉴别人总结的uploadify:基于jquery的文件上传插件,支持ajax无刷新上传,多个文件同时上传,上传进行进度显示,控制文件上传大小,删除已上传文件. uploadify有两个版本,一个用f ...

随机推荐

  1. log4net使用封装,无缝切换 dotnet 和 dotnetcore

    log4net使用封装,无缝切换 dotnet 和 dotnetcore Intro 自己有几个自己的小项目,有许多公用的方法/扩展/工具类等等,于是封装了一些常用的工具类/扩展方法 WeihanLi ...

  2. 深圳市共创力推出独家课程《AHB和OSG》高级实务培训课程!

     <AHB和OSG>高级实务培训课程大纲 [适合对象]:高层管理者.产品经理.资源经理.各专项经理.研发等部门的负责人和骨干员工. [课程受益]:高层如何对项目的优先级进行排序和资源分配. ...

  3. node.js微信小程序配置消息推送

    在开发微信小程序时,有一个消息推送,它的解释是这样的. 消息推送具体的内容是下面的这个网址   https://developers.weixin.qq.com/miniprogram/dev/fra ...

  4. 走进Java Map家族 (1) - HashMap实现原理分析

    在Java世界里,有一个古老而神秘的家族——Map.从底层架构到上层应用,他们活跃于世界的每一个角落.但是,每次出现时,他们都戴着一张冷硬的面具(接口),深深隐藏着自己的内心.所有人都认识他们,却并非 ...

  5. .net c#将数据库数据对象转换为实体值对象

    using System; using System.Data; namespace Sunlib { public static class DataHelper { //将数据库数据对象转换为实体 ...

  6. docker容器安装vi (一般容器都是Debian GNU/Linux 9)

    在使用docker容器时,同时你docker里的系统正好是debian或ubuntu的时候,有时候里边没有安装vim,敲vim命令时提示说:vim: command not found,这个时候就需要 ...

  7. IDEA Can't Update No tracked branch configured for branch master or the branch doesn't exist.

    IDEA Can't Update No tracked branch configured for branch master or the branch doesn't exist.To make ...

  8. 【css】图片垂直水平居中

    一.已知宽高的图片实现垂直水平居中 1.借助margin-top负边距实现垂直居中 <!DOCTYPE html> <html> <head> <meta c ...

  9. TypeScript 之类型判断

    在使用 Angular 做项目的时候,对 TypeScript 的类型判断不太熟练,为了方便查找,特意对 TypeScript 的类型判断做了简单梳理.文章只是 TS 官网的内容摘要,没有高深的知识, ...

  10. 放下VS2010,拥抱VS2019

    VS2019 再过几天就要正式发布了,我们还在使用 VS2010 进行软件开发.由于所处行业环境及项目类型,加之之前的代码积累,所以即使不用新的开发环境.新的语言技术也能比较好的完成工作.那究竟要不要 ...