<!DOCTYPE html>
<html>
<head>
<title>多檔案上傳</title>
<script>
function beforesubmit(form) {
var indexname = form.indexname.value.replace(/\s/g, "");
var upfile = form.upfile.value.replace(/\s/g, "");
if (indexname === "" || upfile === "") {
alert("目錄名稱或上傳檔案不能為空!");
return false;
}
return true;
}
</script>
</head>
<body bgcolor="white">
<br>
<form name="form1" action="upload.php" method="post" enctype="multipart/form-data" onsubmit="return beforesubmit(this)">
<table cellspacing="1" cellpadding="5" border="0" width="580" bgcolor="#C2E2F5">
<tr bgcolor="#E3F2FB" align="center">
<td width="280" height="30">上傳的檔案</td>
</tr>
<?php
// 動態生成 7 個檔案輸入框
for ($i = 1; $i < 8; $i++) {
$upfile = "upfile" . $i;
echo "<tr><td bgcolor=#F5FBFE width=264><input type=file name=$upfile size=25></td></tr>";
}
?>
<tr bgcolor="#E3F2FB">
<td colspan="2" height="1" align="center">
目錄名稱: <input type="text" name="indexname" size="20">
<input type="submit" value="上傳">
</td>
</tr>
</table>
</form>
</body>
</html>
<?php
// upload.php 核心處理邏輯
$uploadDir = './uploads/';
$indexName = basename(trim($_POST['indexname'])); // 防止路徑遍歷
$targetDir = $uploadDir . $indexName . '/';
if (!is_dir($targetDir)) {
mkdir($targetDir, 0755, true);
}
$success = 0;
$errors = [];
for ($i = 1; $i <= 7; $i++) {
$field = "upfile$i";
if (!isset($_FILES[$field]) || $_FILES[$field]['error'] !== UPLOAD_ERR_OK) {
continue; // 未選檔或有錯
}
$file = $_FILES[$field];
$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$allowed = ['jpg','jpeg','png','gif','pdf','doc','docx','xls','xlsx','zip','rar'];
if (!in_array($ext, $allowed)) {
$errors[] = "{$file['name']}: 副檔名不允許";
continue;
}
// 安全檔名:時間戳 + 隨機數 + 原副檔名
$newName = date('YmdHis') . '_' . mt_rand(1000,9999) . '.' . $ext;
$dest = $targetDir . $newName;
if (move_uploaded_file($file['tmp_name'], $dest)) {
$success++;
@chmod($dest, 0644);
} else {
$errors[] = "{$file['name']}: 移動失敗";
}
}
echo "成功上傳 $success 個檔案。";
if ($errors) echo "
錯誤: " . implode("
", $errors);
?>
finfo_file())upload_max_filesize、post_max_size + 程式碼二次檢查basename()、realpath() 驗證最終路徑在允許目錄內php_flag engine off 或 AddHandler 禁止腳本執行<!-- 單一 input 支援多選 -->
<input type="file" name="upfiles[]" multiple accept="image/*,.pdf,.zip" />
<?php
// 後端對應處理
foreach ($_FILES['upfiles']['tmp_name'] as $key => $tmp) {
if ($_FILES['upfiles']['error'][$key] === UPLOAD_ERR_OK) {
// 處理單檔...
}
}
?>
大檔案建議使用:
後端需支援分片合併、MD5 校驗、暫存目錄管理。