php lumen获取文件流,lumen 支持多文件上传及php 原生多文件上传

1、webform (注意:name后面一定要加[]号)

First name:

First name:

Last name:

注:HTML5的文件已经支持多文件上传:

2、后端处理

1 /*上传文件到服务器*/

2 public function uploadFile(\Illuminate\Http\Request $request)3 {4

5 $this->validate($request,array('token'=>'required|string'));6 $this->validationUser($uid,$this->authService);7 if($request->hasFile('file')) {8 $root = $request->server('DOCUMENT_ROOT');9 $file = $request->file('file');10 //return var_dump($file);

11 $filePath=[]; //定义空数组用来存放图片路径

12 foreach ($file as $k => $v) {13 //判断图片上传中是否出错

14 if (!$v->isValid()) {15 $this->apiReturn("上传图片出错,请重试!",1);16 }17 //此处防止没有多文件上传的情况

18 if(!empty($v)){19 if ($v->getSize() / 1024 > 600)20 return $this->apiReturn("请检查您上传的文件不能大于600KB", 1);21 $fileName = strtolower($v->getClientOriginalName());22 if (!preg_match('/\.(jpg|jpeg|png|gif)$/', $fileName))23 return $this->apiReturn("您只能上传通用的图片格式", 1);24 $destinationPath = '/mobile/resources/uploads/' . date('Ymd');25 $fileExtendName = substr($fileName, strpos($fileName, '.'));26 $realPath = $root . $destinationPath;27 if (!file_exists($realPath))28 mkdir($realPath);29 $newFileName = uniqid() . mt_rand(1, 100000) . $fileExtendName;30 $v->move($realPath, $newFileName);31 $filePath[]=$destinationPath . '/' . $newFileName;32 }}33 return $this->apiReturn(json_encode($filePath), 0);34 }35 else

36 return $this->apiReturn('请选择文件再上传', 1);37 }

原生多文件上传:

/*多文件上传*/

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

//此处防止没有多文件上传的情况

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'=>'请选择文件再上传'];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值