如何向脚本添加2个文件上传,允许将文件名发送到数据库,将文件发送到目录

时间:2011-10-16 01:42:39

标签: php mysql file-upload

嘿伙计们一直试图改变这个upload.php并且我对此非常新。我需要在脚本中再添加两个文件上传字段,这样我就可以一次上传3个图像到数据库,在那里我可以回显帖子中的文件名并将文件保存到目录中,我不知道如何更改代码以下适合

     upload.php
     <?php
     $link = mysql_connect('localhost', 'root', '');
      if (!$link) {
      die('Could not connect: ' . mysql_error());
      }

      mysql_selectdb("tutorials_upload");
      if(!is_dir("uploads")){//do we need to make the uploads directory for the files?
     mkdir("uploads");//make the rest of the script safe, though this will only be done       once

    }

     function savedata(){
      global $_FILES, $_POST, $putItAt;
     $sql = "INSERT INTO `tutorials_upload`.`thefiles` (
     `ID` ,
     `Time` ,
     `FileLocation` ,
     `IP` ,
     `Title`
      )
      VALUES (
       NULL , UNIX_TIMESTAMP( ) , '".mysql_real_escape_string($putItAt)."',      '".$_SERVER['REMOTE_ADDR']."', '".mysql_real_escape_string($_POST['Title'])."'
      );";
      mysql_query($sql);

     }
        $putItAt = "uploads/".basename($_FILES['uploadedfile']['name']);

      $putItAt = str_replace("php","txt", $putItAt);
     if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$putItAt)){

     savedata();
     header("location: listfiles.php");//redirect them to the listfiles.php page


     }else{
    if(copy($_FILES['uploadedfile']['tmp_name'],$putItAt)){

      savedata();
       header("location: listfiles.php");
    }else{

       echo 'You totally failed. click <a href="index.php">here</a> to go back and try again.';
     }
     }
   ?>

数据库是

      ID,
      Time,
      FileLocation,
      IP and Title

我想将FileLocation2和FileLocation3添加到数据库中 表格是

       <form enctype="multipart/form-data" action="upload.php" method="post">
        Choose your file to upload!
        <input name="uploadedfile" type="file" />
        <br />
         And what would you like to call it? <input name=“title” type=“text” />

         <input type="submit" value="upload file"/>
        </form>   

我需要做的是在表单中再添加2个文件上传字段,并将文件名发布到我将放入数据库的2个图像名称字段谢谢伙伴

1 个答案:

答案 0 :(得分:0)

This会有所帮助。

<form action="file-upload.php" method="post" enctype="multipart/form-data">
      Send these files:<br />
      <input name="userfile[]" type="file" /><br />
      <input name="userfile[]" type="file" /><br />
      <input name="userfile[]" type="file" /><br />
      <input type="submit" value="Send files" />
    </form>

$_FILES['userfile']['name'][0]将包含第一个上传的文件。

$_FILES['userfile']['name'][1]将包含第二个。 。等等。

您可以使用foreach并执行查询。像

这样的东西
foreach ($_FILES['image']['name'] as $i => $name) { }