gpt4 book ai didi

PHP 表单未将所有图像上传到文件夹

转载 作者:行者123 更新时间:2023-11-29 08:09:47 27 4
gpt4 key购买 nike

我有一个 PHP 表单,应该允许用户一次上传 5 个图像。当用户点击提交时,所有图像都应保存到我网站的文件夹 images/ 中。目前,只有第一个图像输入中的图像会保存到 images/ 文件夹中。不过,图像名称都正确保存到我的 MySQL 表中。

这是我的 HTML 表单页面的代码:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<form enctype="multipart/form-data" action="add.php" method="POST">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name = "email"><br>
Phone: <input type="text" name = "phone"><br>
Photo: <input type="file" name="photo"><br>
Photo: <input type="file" name="photo1"><br>
Photo: <input type="file" name="photo2"><br>
Photo: <input type="file" name="photo3"><br>
Photo: <input type="file" name="photo4"><br>
<input type="submit" value="Add">
</form>
</body>
</html>

这是 add.php 页面代码:

<?php 

//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);
$target1 = "images/";
$target1 = $target1 . basename( $_FILES['photo1']['name']);
$target2 = "images/";
$target2 = $target2 . basename( $_FILES['photo2']['name']);
$target3 = "images/";
$target3 = $target3 . basename( $_FILES['photo3']['name']);
$target4 = "images/";
$target4 = $target4 . basename( $_FILES['photo4']['name']);
//This gets all the other information from the form
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$pic=($_FILES['photo']['name']);
$pic1=($_FILES['photo1']['name']);
$pic2=($_FILES['photo2']['name']);
$pic3=($_FILES['photo3']['name']);
$pic4=($_FILES['photo4']['name']);

// Connects to your Database
mysql_connect("dnsacom", "ksbm", "Kszer") or die(mysql_error()) ;
mysql_select_db("keabm") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic', '$pic1', '$pic2', '$pic3', '$pic4')") ;

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your image.";
}
if(move_uploaded_file($_FILES['photo1']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your image.";
}
if(move_uploaded_file($_FILES['photo2']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your image.";
}
if(move_uploaded_file($_FILES['photo3']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your image.";
}
if(move_uploaded_file($_FILES['photo4']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your image.";
}
?>

感谢您的帮助。感谢所有帮助。

最佳答案

它们都有相同的 $target 变量。将 $target 变量更改为 $target1$target2$target3$target4 他们就会上传。

if(move_uploaded_file($_FILES['photo1']['tmp_name'], $target1)) <--- CHANGE THESE

编辑:我测试了您的代码并更改了这些变量,并且能够上传所有图像。

关于PHP 表单未将所有图像上传到文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21867453/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com