首页  >  文章  >  后端开发  >  php 使用html5实现多文件上传实例

php 使用html5实现多文件上传实例

高洛峰
高洛峰原创
2017-01-14 14:08:361624浏览

首先向大家介绍一下html5中file的multiple属性

定义和用法

multiple 属性规定输入字段可选择多个值。如果使用该属性,则字段可接受多个值。

实例:

<form action="demo_form.asp" method="get">
 Select images: <input type="file" name="img" multiple="multiple" />
 <input type="submit" />
</form>

   

上面实例中的input file 可接受多个文件上传字段。

了解了html5中file的multiple属性,下面我们开始讲解使用html5实现多文件上传。

实例代码:

html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form action="my_parser.php" method="post" enctype="multipart/form-data">
 <p><input name="upload[]" type="file" multiple="multiple" /></p>
 <input type="submit" value="Upload all files">
</form>
</body>
</html>

   

php代码:

for($i=0; $i<count($_FILES[&#39;upload&#39;][&#39;name&#39;]); $i++) {
 //Get the temp file path
 $tmpFilePath = $_FILES[&#39;upload&#39;][&#39;tmp_name&#39;][$i];
 
 //Make sure we have a filepath
 if ($tmpFilePath != ""){
  //Setup our new file path
  $newFilePath = "./uploadFiles/" . $_FILES[&#39;upload&#39;][&#39;name&#39;][$i];
 
  //Upload the file into the temp dir
  if(move_uploaded_file($tmpFilePath, $newFilePath)) {
 
   //Handle other code here
 
  }
 }
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多php 使用html5实现多文件上传实例相关文章请关注PHP中文网!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn