基于HTML5的多文件上传
html5的上传文件标签,支持multiple属性,可以使我们很方便的选择多个文件进行上传。
前台上传文件页面
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>多文件上传实例</title>
</head>
<body>
<div style="width: 800px;height: auto;">
<div>
<form action="upload.php" method="post" target="showInfo" enctype="multipart/form-data">
<input type="file" name="files[]" multiple="multiple"/><input type="submit" value="上传">
</form>
</div>
<div style="width: 100%;height: 200px;">
<iframe name="showInfo" src="about:blank" width="100%" height="100%"></iframe>
</div>
</div>
</body>
</html>
后台获取的$_FILES
数组格式
array (size=1)
'files' =>
array (size=5)
'name' =>
array (size=2)
0 => string 'QQ截图20161101202109.png' (length=26)
1 => string 'test.html' (length=9)
'type' =>
array (size=2)
0 => string 'image/png' (length=9)
1 => string 'text/html' (length=9)
'tmp_name' =>
array (size=2)
0 => string 'D:\wamp64\tmp\phpFC7C.tmp' (length=25)
1 => string 'D:\wamp64\tmp\phpFC7D.tmp' (length=25)
'error' =>
array (size=2)
0 => int 0
1 => int 0
'size' =>
array (size=2)
0 => int 3587
1 => int 75
后台文件上传处理程序
upload.php
<?php
/**
* Created by PhpStorm.
* User: zhezhao
* Date: 2016/11/2
* Time: 9:45
*/
$res = [];
for($i=0;$i<count($_FILES["files"]["name"]);$i++){
$tmp = explode(".",$_FILES["files"]["name"][$i]);
$suffix_name = end($tmp);
$path = __DIR__.DIRECTORY_SEPARATOR.time().".".$suffix_name;
if(move_uploaded_file($_FILES["files"]["tmp_name"][$i],$path)){
$res[] = $path;
}
}
echo "upload ".count($res)." files.";
foreach ($res as $path){
echo "<p>".$path."</p>";
}
基于FLASH的多文件上传
参考文档: