我有一个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.";
}
?>
感谢您的帮助。感谢所有帮助。
答案 0 :(得分:1)
也许检查地图权限,我建议检查chmod,我在你的代码中找不到问题。
http://nl1.php.net/chmod 或者你上传多个文件后使用括号?喜欢type =“file []”
OW
if(move_uploaded_file($_FILES['photo1']['tmp_name'], $target))
问题解决了你到处使用$ target的原因,这就是为什么只有1张图片上传的原因
答案 1 :(得分:1)
它们都具有相同的$target
变量。将您的$target
变量更改为$target1
,$target2
,$target3
和$target4
,然后上传。
if(move_uploaded_file($_FILES['photo1']['tmp_name'], $target1)) <--- CHANGE THESE
编辑:我测试了您的代码并更改了这些变量,并且能够上传所有图片。