dongsou4301 2011-10-26 12:24
浏览 59
已采纳

获取多个文件上载文件的名称

I have been trying to upload multiple files into a folder and save the names in a database table. Here's the code: I need to get the names of those uploaded files. I know my code is crap :(

Is there a way to loop through uploading those files and still return the file names? I need those names to be able to insert them into mysql table.

Thanks for your assistance.

<form action="file-upload.php" method="post" enctype="multipart/form-data">
  Send these files:<br />
  <input name="pic1" type="file" /><br />
  <input name="pic2" type="file" /><br />
  <input name="pic3" type="file" /><br />
  <input name="pic4" type="file" /><br />
  <input type="submit" value="Send files" />
</form>

And the PHP script is below:

<?php

  $target = "uploads/"; 
  $target = $target . basename( $_FILES['pic1']['name']); 
  $target = $target . basename( $_FILES['pic2']['name']); 
  $target = $target . basename( $_FILES['pic3']['name']); 
  $target = $target . basename( $_FILES['pic4']['name']); 

 $pic1 =($_FILES['pic1']['name']); 
 $pic2 =($_FILES['pic2']['name']); 
 $pic3 =($_FILES['pic3']['name']); 
 $pic4 =($_FILES['pic4']['name']); 

$con = mysql_connect("localhost", "root", "");
  if (!$con){
    die('Could not connect: ' . mysql_error());
   }

   mysql_select_db("people", $con);

   $sql="INSERT INTO mtpupload (name, age, pic1, pic2, pic3, pic4 )
   VALUES('$_POST[name]','$_POST[age]','$pic1', '$pic2', '$pic3', '$pic4')";

  //---------Here, I want to insert all the pictures----------//    
    if(move_uploaded_file($_FILES['pic1']['tmp_name'], $target)) { 
       //do nothing
     }
     if(move_uploaded_file($_FILES['pic2']['tmp_name'], $target)) { 
       //do nothingelse{
     }if(move_uploaded_file($_FILES['pic3']['tmp_name'], $target)) { 
       //do nothing  echo "Sorry, the image was not moved from temp folder.";
     }if(move_uploaded_file($_FILES['pic4']['tmp_name'], $target)) { 
       //do nothing   
       echo "The was a problem uploading one of your images.";
     }
    //--------------Ends here---------------------//

    if (!mysql_query($sql,$con)){
    die('Error: ' . mysql_error());
    }
    echo "1 record added";

   mysql_close($con)
   ?> 

UPDATE: turns out that this code is working but only saving one image in the folder. I think my move_uploaded_file is wrong. Any pointers?

Thanks again for your help.

  • 写回答

3条回答 默认 最新

  • duanshan1977 2011-10-26 13:36
    关注

    Thanks folks for your suggestions, I finally got it working. I was using $target variable in many places. Renaming it to different variable helped.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题