lumen 支持多文件上传及php 原生多文件上传
生活随笔
收集整理的這篇文章主要介紹了
lumen 支持多文件上传及php 原生多文件上传
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1、webform (注意:name后面一定要加[]號(hào))
<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的文件已經(jīng)支持多文件上傳:
<input type="file" name="file[]" multiple/>2、后端處理
1 /*上傳文件到服務(wù)器*/ 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=[]; // 定義空數(shù)組用來存放圖片路徑 12 foreach ($file as $k => $v) { 13 // 判斷圖片上傳中是否出錯(cuò) 14 if (!$v->isValid()) { 15 $this->apiReturn("上傳圖片出錯(cuò),請重試!",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=[]; // 定義空數(shù)組用來存放圖片路徑$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);}elsereturn ['error'=>'請選擇文件再上傳'];}
轉(zhuǎn)載于:https://www.cnblogs.com/fogwang/p/10399159.html
總結(jié)
以上是生活随笔為你收集整理的lumen 支持多文件上传及php 原生多文件上传的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP 实现归并排序算法
- 下一篇: 为何说国内云桌面已经步入成熟期