Paste_Image.png
test.php
<html>
<head><title></title></head>
<body>
<form action="doAction.php" method="post" enctype="multipart/form-data">
請選擇您要上傳的文件:<input type="file" name="myFile[]"><br/>
請選擇您要上傳的文件:<input type="file" name="myFile[]"><br/>
請選擇您要上傳的文件:<input type="file" name="myFile[]"><br/>
請選擇您要上傳的文件:<input type="file" name="myFile[]" multiple="multiple"><br/>
<input type="submit" value="上傳文件">
</form>
</body>
</html>
common.func.php
<?php
/**
* 得到文件擴展名
* @param $filename
* @return string
*/
function getExt($filename)
{
return strtolower(pathinfo($filename, PATHINFO_EXTENSION));
}
/**
* 產生唯一字符串
* @return string
*/
function getUniName()
{
return md5(uniqid(microtime(true), true));
}
upload.func.php
<?php
/**
* 構建上傳文件信息
* @return mixed
*/
function getFiles()
{
$i = 0;
foreach ($_FILES as $file) {
if (is_string($file['name'])) {
//單文件
$files[$i] = $file;
$i++;
} else if (is_array($file['name'])) {
foreach ($file['name'] as $key => $val) {
$files[$i]['name'] = $file['name'][$key];
$files[$i]['type'] = $file['type'][$key];
$files[$i]['tmp_name'] = $file['tmp_name'][$key];
$files[$i]['error'] = $file['error'][$key];
$files[$i]['size'] = $file['size'][$key];
$i++;
}
}
}
return $files;
}
function uploadFile($fileInfo, $path = './uploads', $flag = true,
$maxSize = 1048576, $allowExt = array('jpeg', 'jpg', 'gif', 'png'))
{
$res = null;
//判斷錯誤號
if ($fileInfo['error'] === UPLOAD_ERR_OK) {
if ($fileInfo['size'] > $maxSize) {
$res['mes'] = $fileInfo['name'] . '上傳文件過大';
}
$ext = getExt($fileInfo['name']);
if (!in_array($ext, $allowExt)) {
$res['mes'] = $fileInfo['name'] . '非法文件類型';
}
if ($flag && !$res['mes']) {
if (!getimagesize($fileInfo['tmp_name'])) {
$res['mes'] = $fileInfo['name'] . "不是真實圖片類型";
}
}
if (!is_uploaded_file($fileInfo['tmp_name'])) {
$res['mes'] = $fileInfo['name'] . "文件不是通過HTTP POST方式上傳來的";
}
if ($res['mes']) {
return $res['mes'];
}
if (!file_exists($path)) {
mkdir($path, 0777, true);
chmod($path, 0777);
}
$uniName = getUniName();
$destination = $path . '/' . $uniName . '.' . $ext;
if (move_uploaded_file($fileInfo['tmp_name'], $destination)) {
$res['mes'] = $fileInfo['name'] . "文件移動失敗";
}
$res['mes'] = $fileInfo['name'] . '上傳成功';
$res['dest'] = $destination;
return $res;
} else {
switch ($fileInfo['error']) {
case 1:
$res['mes'] = "上傳文件超過了PHP配置文件中upload_max_file";
break;
case 2:
$res['mes'] = "超過了表單MAX_FILE_SIZE";
break;
case 3:
$res['mes'] = "文件部分被上傳";
break;
case 4:
$res['mes'] = "沒有選擇上傳文件";
break;
case 6:
$res['mes'] = "沒有找到臨時目錄";
break;
case 7:
case 8:
$res['mes'] = "系統錯誤";
break;
}
return $res['mes'];
}
}
doAction.php
<?php
//foreach ($_FILES as $fileInfo) {
// $files[] = uploadFile($fileInfo);
//}其中有文件不符合類型會exit (file1 file2 file3 file4)
//var_dump($_FILES);
header('content-type:text/html;charset=utf-8');
require_once 'upload.func.php';
require 'common.func.php';
$files = getFiles();
//var_dump($files);
foreach ($files as $fileInfo) {
$res = uploadFile($fileInfo);
echo $res['mes'], "<br/>";
$uploadFiles[] = $res['dest'];
}
$uploadFiles = array_values(array_filter($uploadFiles));
var_dump($uploadFiles);
?>
Paste_Image.png
Paste_Image.png