使用PHP以一种形式从输入字段上传多个文件?

时间:2016-11-14 09:05:59

标签: php file file-upload upload multiple-columns

    <form action="upload.php" method="post" enctype="multipart/form-data">
        <table border="0">
          <tr>
            <td>Select to upload:</td>
          </tr>
          <tr>
            <td>Image *</td>
            <td>File *</td>
            <td>File 1 *</td>
            <td>File 2 *</td>
            <td>File 3</td>
            <td>File 4</td>
          </tr>
          <tr>
            <td><input type="file" name="image" multiple="multiple" ></td>
            <td><input type="file" name="file1" multiple="multiple" ></td>
            <td><input type="file" name="file2" multiple="multiple" ></td>
            <td><input type="file" name="file3" multiple="multiple" ></td>
            <td><input type="file" name="file4" multiple="multiple" ></td>
            <td><input type="file" name="file5" multiple="multiple" ></td>
          </tr>
          <tr>
            <i class="glyphicon glyphicon-upload">
                <td><input type="submit" value="Start Upload" name="submit"></td>
            </i>
          </tr>
        </table>
    </form>  // Closes the new window.

    <?php
    include "dbConfig.php";
    session_start();
    if($_SESSION['username'] == ''){
        header('Location: user.php');
    }
    if(isset($_POST["submit"])) {
        $target_dir = "uploads/";
        $target_image = $target_dir . ($_SESSION['username'].'_'.basename($_FILES["image"]["name"]));
        $target_file1 = $target_dir . ($_SESSION['username'].'_'.basename($_FILES["file1"]["name"]));
        $target_file2 = $target_dir . ($_SESSION['username'].'_'.basename($_FILES["file2"]["name"]));
        $target_file3 = $target_dir . ($_SESSION['username'].'_'.basename($_FILES["file3"]["name"]));
        $target_file4 = $target_dir . ($_SESSION['username'].'_'.basename($_FILES["file4"]["name"]));
        $target_file5 = $target_dir . ($_SESSION['username'].'_'.basename($_FILES["file5"]["name"]));

        $uploadOk = 1;
        $imageFileType = pathinfo($target_image,PATHINFO_EXTENSION);


        if ($_FILES["image"]["size"] == 0 || $_FILES["file1"]["size"] == 0 ||$_FILES["file2"]["size"] == 0 || $_FILES["file3"]["size"] == 0) {
            echo "You Must upload Fist 4 files <br>";
            $uploadOk = 0;}
else{
            if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
            && $imageFileType != "gif" ) {
                echo "<center><h2>Sorry, only JPG, JPEG, PNG & GIF files are allowed for Image.<br></center></h2>";
                $uploadOk = 0;
            }
            if ($uploadOk == 0) {
                echo "<center><h2>Sorry, your file was not uploaded.</center></h2>";
            } else {
                if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_image) && move_uploaded_file($_FILES["file1"]["tmp_name"], $target_file1) && move_uploaded_file($_FILES["file2"]["tmp_name"], $target_file2) && move_uploaded_file($_FILES["file3"]["tmp_name"], $target_file3)) {

                    $img = $_SESSION['username']."_".basename( $_FILES["image"]["name"]);
                    $file1 = $_SESSION['username']."_".basename( $_FILES["file1"]["name"]);
                    $file2 = $_SESSION['username']."_".basename( $_FILES["file2"]["name"]);
                    $file3 = $_SESSION['username']."_".basename( $_FILES["file3"]["name"]);
                    if($_FILES["file4"]["size"] == 0){
                            $file4 = '';
                    }else{
                            move_uploaded_file($_FILES["file4"]["tmp_name"], $target_file4);
                            $file4 = $_SESSION['username']."_".basename( $_FILES["file4"]["name"]);
                    }
                    if($_FILES["file5"]["size"] == 0){
                            $file5 = '';
                    }else{
                            move_uploaded_file($_FILES["file5"]["tmp_name"], $target_file5);
                            $file5 = $_SESSION['username']."_".basename( $_FILES["file5"]["name"]);
                    }
                    $sql = "INSERT INTO user_data(name, image, file1, file2, file3, file4, file5) VALUES ('".$_SESSION['username']."','".$img."','".$file1."','".$file2."','".$file3."','".$file4."','".$file5."')";
                    $query = mysql_query($sql);
                    echo "The file has been uploaded.";

                } else {
                    echo "Sorry, there was an error uploading your file.";
                }
            }
        }
    }
    ?>

1 个答案:

答案 0 :(得分:0)

我建议使用函数/循环将所有文件上传到目录。 对于拼写错误,答案尚未得到解决 - 也许你会仔细查看它并测试它是否合适。

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Modal Example</h2>


 <li>
      <a href="#" data-toggle="modal" data-target="#myModal"><span class="glyphicon glyphicon-user"></span> <asp:Label runat="server" ID="lblUserName"></asp:Label></a>
</li>

<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Modal Header</h4>
      </div>
      <div class="modal-body">
        <p>Some text in the modal.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>





  </div>

</body>
</html>