方式1代码
<!DOCTYPE html>
<html lang="en">
<?php
foreach ($_FILES as $file) {
if ($file['error'] === 0) {
$destFile = 'uploads/' . $file['name'];
move_uploaded_file($file['tmp_name'],$destFile);
echo "<img src='$destFile' width='230'>";
}
}
?>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>多文件上传</title>
<style>
.upload {
display: grid;
grid-template-columns: repeat(1,1fr);
gap: 3vw;
padding: 0 15%;
}
.upload button {
width: 120px;
}
</style>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<fieldset class="upload">
<legend>多文件上传:逐个上传</legend>
<input type="file" name="mydoc1">
<input type="file" name="mydoc2">
<input type="file" name="mydoc3">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
方式2代码
<!DOCTYPE html>
<html lang="en">
<?php
printf('<pre>%s</pre>', print_r($_FILES, true));
if (isset($_FILES['mypic'])) {
foreach ($_FILES['mypic']['error'] as $key => $error) {
if ($error === 0) {
$tmpName = $_FILES['mypic']['tmp_name'][$key];
$name = $_FILES['mypic']['name'][$key];
$destFile = 'uploads/' . $name;
move_uploaded_file($tmpName,$destFile);
echo "<img src='$destFile' width='240'>";
}
}
}
?>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document多文件上传2</title>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>多文件上传:逐个上传2</legend>
<input type="file" name="mypic[]">
<input type="file" name="mypic[]">
<input type="file" name="mypic[]">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
方式3代码
<!DOCTYPE html>
<html lang="en">
<?php
printf('<pre>%s</pre>', print_r($_FILES, true));
if (isset($_FILES['mypic'])) {
foreach ($_FILES['mypic']['error'] as $key => $error) {
if ($error === 0) {
$tmpName = $_FILES['mypic']['tmp_name'][$key];
$name = $_FILES['mypic']['name'][$key];
$destFile = 'uploads/' . $name;
move_uploaded_file($tmpName,$destFile);
echo "<img src='$destFile' width='240'>";
}
}
}
?>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document多文件上传3</title>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>多文件上传:批量上传</legend>
<input type="file" name="mypic[]" multiple>
<button>上传</button>
</fieldset>
</form>
</body>
</html>