PHP - 多维数组输入(具有文件类型和文本类型)表单提交

时间:2014-10-28 19:04:29

标签: php arrays forms codeigniter multidimensional-array

查看

<div>
   <input type='file' name="active_board_data[pictures][]">
   <input type='text' name="active_board_data[name][]">
   <input type='text' name="active_board_data[description][]">
</div>
<div>
   <input type='file' name="active_board_data[pictures][]">
   <input type='text' name="active_board_data[name][]">
   <input type='text' name="active_board_data[description][]">
</div>
<div>
   <input type='file' name="active_board_data[pictures][]">
   <input type='text' name="active_board_data[name][]">
   <input type='text' name="active_board_data[description][]">
</div>

提交后var_dump($_POST['active_board_data']);显示

array (size=3)
  'name_description_input' => 
    array (size=3)
      0 => string '' (length=0)
      1 => string '<p>sdfv</p>' (length=11)
      2 => string '' (length=0)
  'relevant_details_description_input' => 
    array (size=3)
      0 => string '<p>dsrvsd</p>' (length=13)
      1 => string '' (length=0)
      2 => string '<p>sdfvr</p>' (length=12)
  'pictures' => 
    array (size=1)
      0 => string '' (length=0)

var_dump($_FILES['active_board_data']);显示

array (size=5)
  'name' => 
    array (size=1)
      'pictures' => 
        array (size=3)
          0 => string 'active_trustees_26-Oct-2014__08_10_00__25408.jpg' (length=48)
          1 => string 'active_trustees_26-Oct-2014__08_10_18__7037.jpg' (length=47)
          2 => string 'active_trustees_26-Oct-2014__08_10_18__3779.jpg' (length=47)
  'type' => 
    array (size=1)
      'pictures' => 
        array (size=3)
          0 => string 'image/jpeg' (length=10)
          1 => string 'image/jpeg' (length=10)
          2 => string 'image/jpeg' (length=10)
  'tmp_name' => 
    array (size=1)
      'pictures' => 
        array (size=3)
          0 => string 'C:\wamp\tmp\php128F.tmp' (length=23)
          1 => string 'C:\wamp\tmp\php12A0.tmp' (length=23)
          2 => string 'C:\wamp\tmp\php12B0.tmp' (length=23)
  'error' => 
    array (size=1)
      'pictures' => 
        array (size=3)
          0 => int 0
          1 => int 0
          2 => int 0
  'size' => 
    array (size=1)
      'pictures' => 
        array (size=3)
          0 => int 49543
          1 => int 52657
          2 => int 29884

这是在表单提交后在php中的同一个数组上同时拥有文件和文本的正确方法吗?或者我应该更改标记? 注意:我正在动态创建 div 。请帮助。谢谢。 这是提交表格的图片: enter image description here

1 个答案:

答案 0 :(得分:2)

您仍然可以同时访问这两者,但不使用foreach结构,而是使用for结构,如下所示:

$szFiles = sizeof ($_FILES['active_board_data']['name']['pictures']);
for ($i=0; $i<$szFiles; $i++)
{
    $file_name   = $_FILES['active_board_data']['name']['pictures'][$i];
    $name        = $_POST['active_board_data']['name'][$i];
    $description = $_POST['active_board_data']['description'][$i];
}

暂且不说:对我而言,框架的最佳部分是使用它的一些辅助功能,例如CI文件上传器帮助程序系统:https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html