PHP上传⽂件DEMO <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>upload</title>
</head>
<body>
<!--
**********⽂件上传测试*************
form加上enctype="multipart/form-data" method="post"
input type改为fileinputtypefile不上传文件
**********⽂件上传测试*************
-->
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="test[]"/>
<input type="file" name="test[]"/>
<div></div>
<input type="submit" value="submit"/>
</form>
</body>
</html>
index.php
<?php
// 所有的上传⽂件都缓存在关系数组$FILES⾥⾯,循环可获取
/*
* 这⾥⽀持单个⽂件上传、多个⽂件上传(⾮单个控件多选⽂件,是多个控件选中多个⽂件)
* <input type="file" name="test"/>
* <input type="file" name="test"/>
* ⽀持控件名为 name="name" 和name="name[]"的多⽂件上传
* <input type="file" name="test[]"/>
* <input type="file" name="test[]"/>
*/
foreach ($_FILES as$key => $value) {
/
/ 获取⽂件名
$names = $value["name"];
// 如果⽂件名是个数组的话就是多⽂件上传
if (is_array($names)) {
for ($i = 0; $i < count($names); $i++) {
copyFile($names[$i], $value['tmp_name'][$i], $value["error"][$i]);
}
} else copyFile($names, $value['tmp_name'], $value["error"]);
}
// ⽂件名,临时⽂件名,错误码
function copyFile($name, $tmpName, $error) {
/
/ 错误码⼤于0,直接返回
if ($error > 0) return;
// 如果⽂件已经存在,输⼊提⽰信息,不保存
if (file_exists($name)) {
echo$name . " already exists. ";
} else { // 否则正常保存⽂件
move_uploaded_file($tmpName, $name);
echo "Stored in: " . dirname(__FILE__) . '\\' . $name;
}
}
?>
upload.php
两个⽂件置于同⼀个⽂件夹中,发布在apache htdocs⽂件夹中访问