将文件从单个HTML表单上传到PHP以获取“多个”不同的文件输入?

时间:2013-12-18 18:16:03

标签: php forms file-upload

我有一个简单的页面,每个区域有9个区块区域 - 用户可以为“菜单”选择图像,为该区域的“内容”选择另一个图像。

然后,他们可以使用提交按钮“上传此区域的文件”上传这些图像。

页面顶部还有一个“保存”/提交。

页面使用了一个大的表单(每个区域都有很多个较小的表单,但是我可以将其设置为1表单,因为我需要一次提交所有内容)。

我的HTML页面基本上是:

<!DOCTYPE html>
<html>
<head>
  <title>Testing</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <!-- Bootstrap -->
  <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
  <link href="css/zoombox.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>
  <div class="container">
    <div class="well well-large">
      <h1>Configuration page</h1>
    </div>

    <form action="index.php" method="post" enctype="multipart/form-data">
      <div>
        <div class="row">
          <div class="span4">
            <button class="btn btn-primary" type="submit">Save</button>
          </div>
        </div>
      </div>

      <div class="rowspace row">
        <div class="column span3">
          <h4>Content: 1</h4>

            <input type="file" name="fileData[1][home]" id="fileData[1][home]" title="Select MENU image"><br>

            <input type="file" name="fileData[1][content]" id="fileData[1][content]" title="Select CONTENT image"><br>

            <input type="submit" class="btn btn-info" name="submit" value="Upload">

        </div>

        <div class="column offset1 span3">
          <h4>Content: 2</h4>

            <input type="file" name="fileData[2][home]" id="fileData[2][home]" title="Select MENU image"><br>

            <input type="file" name="fileData[2][content]" id="fileData[2][content]" title="Select CONTENT image"><br>

            <input type="submit" class="btn btn-info" name="submit" value="Upload">

        </div>

        <div class="column offset1 span3">
          <h4>Content: 3</h4>

            <input type="file" name="fileData[3][home]" id="fileData[3][home]" title="Select MENU image"><br>

            <input type="file" name="fileData[3][content]" id="fileData[3][content]" title="Select CONTENT image"><br>

            <input type="submit" class="btn btn-info" name="submit" value="Upload">

        </div>
      </div>

      <div class="rowspace row">
        <div class="column span3">
          <h4>Content: 4</h4>

            <input type="file" name="fileData[4][home]" id="fileData[4][home]" title="Select MENU image"><br>

            <input type="file" name="fileData[4][content]" id="fileData[4][content]" title="Select CONTENT image"><br>

            <input type="submit" class="btn btn-info" name="submit" value="Upload">

        </div>

        <div class="column offset1 span3">
          <h4>Content: 5</h4>

            <input type="file" name="fileData[5][home]" id="fileData[5][home]" title="Select MENU image"><br>

            <input type="file" name="fileData[5][content]" id="fileData[5][content]" title="Select CONTENT image"><br>

            <input type="submit" class="btn btn-info" name="submit" value="Upload">

        </div>

        <div class="column offset1 span3">
          <h4>Content: 6</h4>

            <input type="file" name="fileData[6][home]" id="fileData[6][home]" title="Select MENU image"><br>

            <input type="file" name="fileData[6][content]" id="fileData[6][content]" title="Select CONTENT image"><br>

            <input type="submit" class="btn btn-info" name="submit" value="Upload">

        </div>
      </div>

      <div class="rowspace row">
        <div class="column span3">
          <h4>Content: 7</h4>

            <input type="file" name="fileData[7][home]" id="fileData[7][home]" title="Select MENU image"><br>

            <input type="file" name="fileData[7][content]" id="fileData[7][content]" title="Select CONTENT image"><br>

            <input type="submit" class="btn btn-info" name="submit" value="Upload">

        </div>

        <div class="column offset1 span3">
          <h4>Content: 8</h4>

            <input type="file" name="fileData[8][home]" id="fileData[8][home]" title="Select MENU image"><br>

            <input type="file" name="fileData[8][content]" id="fileData[8][content]" title="Select CONTENT image"><br>

            <input type="submit" class="btn btn-info" name="submit" value="Upload">

        </div>

        <div class="column offset1 span3">
          <h4>Content: 9</h4>

            <input type="file" name="fileData[9][home]" id="fileData[9][home]" title="Select MENU image"><br>

            <input type="file" name="fileData[9][content]" id="fileData[9][content]" title="Select CONTENT image"><br>

            <input type="submit" class="btn btn-info" name="submit" value="Upload">

        </div>
      </div>

      <div class="rowspace row">
        <div class="column span3">
          <h4>Map Content</h4>
            <input type="file" name="fileData[10][content]" id="fileData[10][content]" title="Select CONTENT image"><br>

            <input type="submit" class="btn btn-info" name="submit" value="Upload">

        </div>
      </div>
    </form>

    </div>
  </div>

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  <script src="http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js"></script>
  <script src="js/bootstrap.file-input.js"></script>
  <script type="text/javascript" src="js/zoombox.js"></script>
  <script>
  $(document).ready(function(){
    $('input[type=file]').bootstrapFileInput();
    $('.zoombox').zoombox();
  });
  </script>
</body>
</html>

我的 var_dump($ _ FILES)输出为:

<?php

array(1) {
  ["fileData"]=> array(5) {
    ["name"]=> array(10) {
      [1]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [2]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [3]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [4]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [5]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [6]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [7]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [8]=> array(2) {
        ["home"]=> string(5) "8.png" ["content"]=> string(0) ""
      }
      [9]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [10]=> array(1) {
        ["content"]=> string(0) ""
      }

    }
    ["type"]=> array(10) {
      [1]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [2]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [3]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [4]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [5]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [6]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [7]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [8]=> array(2) {
        ["home"]=> string(9) "image/png" ["content"]=> string(0) ""
      }
      [9]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [10]=> array(1) {
        ["content"]=> string(0) ""
      }

    }
    ["tmp_name"]=> array(10) {
      [1]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [2]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [3]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [4]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [5]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [6]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [7]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [8]=> array(2) {
        ["home"]=> string(14) "/tmp/php2DdwXG" ["content"]=> string(0) ""
      }
      [9]=> array(2) {
        ["home"]=> string(0) "" ["content"]=> string(0) ""
      }
      [10]=> array(1) {
        ["content"]=> string(0) ""
      }

    }
    ["error"]=> array(10) {
      [1]=> array(2) {
        ["home"]=> int(4) ["content"]=> int(4)
      }
      [2]=> array(2) {
        ["home"]=> int(4) ["content"]=> int(4)
      }
      [3]=> array(2) {
        ["home"]=> int(4) ["content"]=> int(4)
      }
      [4]=> array(2) {
        ["home"]=> int(4) ["content"]=> int(4)
      }
      [5]=> array(2) {
        ["home"]=> int(4) ["content"]=> int(4)
      }
      [6]=> array(2) {
        ["home"]=> int(4) ["content"]=> int(4)
      }
      [7]=> array(2) {
        ["home"]=> int(4) ["content"]=> int(4)
      }
      [8]=> array(2) {
        ["home"]=> int(0) ["content"]=> int(4)
      }
      [9]=> array(2) {
        ["home"]=> int(4) ["content"]=> int(4)
      }
      [10]=> array(1) {
        ["content"]=> int(4)
      }

    }
    ["size"]=> array(10) {
      [1]=> array(2) {
        ["home"]=> int(0) ["content"]=> int(0)
      }
      [2]=> array(2) {
        ["home"]=> int(0) ["content"]=> int(0)
      }
      [3]=> array(2) {
        ["home"]=> int(0) ["content"]=> int(0)
      }
      [4]=> array(2) {
        ["home"]=> int(0) ["content"]=> int(0)
      }
      [5]=> array(2) {
        ["home"]=> int(0) ["content"]=> int(0)
      }
      [6]=> array(2) {
        ["home"]=> int(0) ["content"]=> int(0)
      }
      [7]=> array(2) {
        ["home"]=> int(0) ["content"]=> int(0)
      }
      [8]=> array(2) {
        ["home"]=> int(210379) ["content"]=> int(0)
      }
      [9]=> array(2) {
        ["home"]=> int(0) ["content"]=> int(0)
      }
      [10]=> array(1) {
        ["content"]=> int(0)
      }

    }

  }

}

我猜我的HTML页面是什么东西?在我尝试将它“重构”为一种形式而不是~20种形式之前,它工作得很好......

1 个答案:

答案 0 :(得分:0)

所以问题是因为我对所有提交按钮都有相同的名称值...将每个提交按钮更改为一个不同的名称,它现在正常工作。