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