dpzzkfb1244 2014-02-18 23:05
浏览 42
已采纳

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

I have a PHP form that is supposed to allow the user to upload 5 images at once. All images should save into my website's folder images/ when the user hits submit. Currently, only the image in the first image input gets saved into the images/ folder. The image names are all saved into my MySQL table correctly, though.

Here is the code for my HTML form page:

<!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>

And here is the add.php page code:

<?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."; 
 } 
 ?> 

Thank you for any help. All help is appreciated.

  • 写回答

2条回答 默认 最新

  • doujuchuan9915 2014-02-18 23:35
    关注

    They all have the same $target variable. Change your $target variables to $target1, $target2, $target3 and $target4 and they'll upload.

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

    Edit: I tested your code and changed these variables and was able to upload all images.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大