我如何上传多个图像与PHP和HTML

  • 本文关键字:图像 PHP HTML php html
  • 更新时间 :
  • 英文 :


我试图创建一个上传表单,用户可以通过3个输入上传3个文件/图像,但不幸的是,我没有足够的PHP知识,使其工作。

我使用的代码是:

https://notepad.pw/xNOPaZSw7nkw3wXCIFHo

我已经修改了您的代码,以便您可以从相同的输入类型上传多个图像

下面是你的index.html文件

<!DOCTYPE html>
<html lang="en">
<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</title>
</head>
<body>
<form action="upload-script.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload[]" id="fileToUpload" multiple="multiple">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>

这将是你的upload-script.php文件

<?php 
if (isset($_FILES['fileToUpload'])) {
$total = count($_FILES['fileToUpload']['name']);
$target_dir = "uploads/";    
$uploadOk = 1;

// Check if image file is a actual image or fake image
for( $i=0 ; $i < $total ; $i++ ) { 
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"][$i]);
if($check !== false) {
echo "<br>File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "<br>File is not an image.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"][$i]);
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"][$i], $target_file)) {
echo "<br>The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"][$i])). " has been uploaded.";
} else {
echo "<br>Sorry, there was an error uploading your file.";
}
}
} 
?>

如果你只想上传3张图片,那么你可以根据需要修改$total变量为for循环。

同样,如果你需要3个单独的文件选择器,你可以检查修改后的代码。

index.html
<!DOCTYPE html>
<html lang="en">
<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</title>
</head>
<body>
<form action="upload-script2.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload1" id="fileToUpload1" required>
<input type="file" name="fileToUpload2" id="fileToUpload2" required>
<input type="file" name="fileToUpload3" id="fileToUpload3" required>
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>

这将是你的upload-script.php

<?php
if(isset($_POST['submit'])){
if (isset($_FILES['fileToUpload1'])) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload1"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image

$check = getimagesize($_FILES["fileToUpload1"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload1"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload1"]["name"])). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}

} 
if (isset($_FILES['fileToUpload2'])) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload2"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image

$check = getimagesize($_FILES["fileToUpload2"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload2"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload2"]["name"])). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}

}
if (isset($_FILES['fileToUpload3'])) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload3"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image

$check = getimagesize($_FILES["fileToUpload3"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload3"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload3"]["name"])). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}

}
}
?>

您可以删除图像的isset检查,因为我已经制作了所需的所有图像。如果你从html中删除了必需的attr,那么你将需要isset checks

最新更新