我想上传多个文件而不是一个。我知道如何上传单个文件(图片) 我需要帮助上传多个文件。您可以在下面看到我如何上传单个文件。
这是我添加时的HTML:
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" multiple><br>
<input type="submit" name="submit" value="Submit">
</form>
我看到有些人将name="file"更改为name="file[]"。但我不知道如何用php上传它。
这是PHP代码
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
$random_digit=rand(0000,9999);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
$file_name = $_FILES["file"]["name"];
$ext = pathinfo($file_name, PATHINFO_EXTENSION); // Get the extension of a file
$new_file_name = $random_digit . '.' . $ext;
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $new_file_name);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>