PHP文件上传
通过文件上传的例子,了解文件上传的过程以及所需要的php中的$_FILES全局数组的使用
PHP 的全局数组 $_FILES
第一个参数是表单的 input name,第二个下标可以是 “name”、“type”、“size”、“tmp_name” 或 “error”。如下所示
$_files全局数组 | 意义 |
---|---|
$_FILES[“file”][“name”] | 上传文件的名称 |
$_FILES[“file”][“type”] | 上传文件的类型 |
$_FILES[“file”][“size”] | 上传文件的大小、以字节记 |
$_FILES[“file”][“tmp_name”] | 存储在服务器的文件的临时副本的名称 |
$_FILES[“file”][“error”] | 由文件上传导致的错误代码 |
表单结构
<!--文件名称 manyFilee.php-->
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>多文件上传</title>
</head>
<body>
<form action="manyFiles_ok.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>内容1</td>
<td width="369">
<input type="file" name="picture[]" size="30">
</td>
</tr>
<tr>
<td>内容2</td>
<td width="369">
<input type="file" name="picture[]" size="30">
</td>
</tr>
<tr>
<td>内容3</td>
<td width="369">
<input type="file" name="picture[]" size="30">
</td>
</tr>
<tr>
<td>内容4</td>
<td width="369">
<input type="file" name="picture[]" size="30">
</td>
</tr>
<tr>
<td colspan="2">
<button type="submit">提交</button>
</td>
</tr>
</table>
</form>
</body>
</html>
php上传文件
<?php
//判断文件上传的目录
if (!is_dir("./upfile")) {
mkdir("./upfile");
}
//去除文件中相同的文件名
$array = array_unique($_FILES["picture"]["name"]);
//上传文件名
foreach ($array as $k => $v) {
$path = "upfile/" . $v;
if ($v) {
if (move_uploaded_file($_FILES["picture"]["tmp_name"][$k], $path)) {
$result = true;
} else {
$result = false;
}
}
}
//判断文件是否上传成功
if ($result == true) {
echo "文件上传成功,请稍等......";
echo "<meta http-equiv=\"refresh\" content=\"3,url=manyFiles.php\">";
} else {
echo "文件上传失败,请稍等......";
echo "<meta http-equiv=\"refresh\" content=\"3,url=manyFiles.php\">";
}