【问题标题】:php segmented file uploads seperated in arrayphp分段文件上传以数组分隔
【发布时间】:2020-01-07 05:01:49
【问题描述】:

我已经创建了一个动态注册表 - 并且与我需要能够上传文件的片段之一相关。

这一切都像花花公子一样工作。除了文件上传。

做一个 print_r($_FILES);尝试上传后给出:

    [0]=>
     array(5) {
        ["name"]=>
        array(2) {
  [0]=>
  string(16) "image001 (3).jpg"
  [1]=>
  string(16) "image001 (2).jpg"
}
["type"]=>
array(2) {
  [0]=>
  string(10) "image/jpeg"
  [1]=>
  string(10) "image/jpeg"
}
["tmp_name"]=>
array(2) {
  [0]=>
  string(14) "/tmp/phpdqCWSl"
  [1]=>
  string(14) "/tmp/phpabqqwj"
}
["error"]=>
array(2) {
  [0]=>
  int(0)
  [1]=>
  int(0)
}
["size"]=>
array(2) {
  [0]=>
  int(1288)
  [1]=>
  int(1288)
}

} }

这里将两个上传合并到同一个数组中,而不是使两个子数组的数组大小。

<td style='display:none;' class=td_register_purchases>
    <input type=text name=registration[][purchase_data] id=purchase_data class=purchase_data>
    <select name='registration[][purchase_type]' id=select_purchase class=purchase_type>
        <option value=1>Accomodation</option>
        <option value=2>Catering</option>
        <option value=3>Tools</option>
        <option value=4>Software/License</option>
        <option value=5>Hardware</option>
    </select>
    <textarea rows=2 cols=50 name='registration[][purchase_comment]' class=purchase_comment placeholder='Comment Area'></textarea>
    <input type=file name='upload_file[]' class='upload-file'> Attachment
</td>

我可以处理这个结果:

  [0]=>
  array(5) {
    ["name"]=>
    array(1) {
      [0]=>
      string(16) "image001 (3).jpg"
    }
    ["type"]=>
    array(1) {
      [0]=>
      string(10) "image/jpeg"
    }
    ["tmp_name"]=>
    array(1) {
      [0]=>
      string(14) "/tmp/phpdqCWSl"
    }
    ["error"]=>
    array(1) {
      [0]=>
      int(0)
    }
    ["size"]=>
    array(1) {
      [0]=>
      int(1288)
    }
  }

  [1]=>
  array(5) {
    ["name"]=>
    array(1) {
      [0]=>
      string(16) "image001 (2).jpg"
    }
    ["type"]=>
    array(1) {
      [0]=>
      string(10) "image/jpeg"
    }
    ["tmp_name"]=>
    array(1) {
      [0]=>
      string(14) "/tmp/phpabqqwj"
    }
    ["error"]=>
    array(1) {
      [0]=>
      int(0)
    }
    ["size"]=>
    array(1) {
      [0]=>
      int(1288)
    }
  }
}

但我可能在某个地方犯了一个可以解决所有问题的错误。

【问题讨论】:

    标签: php arrays file input


    【解决方案1】:

    努力工作.. 并找到了一个 - 不漂亮的解决方案 ;-)

        $number_files = sizeof($_FILES["upload_file"]["name"]);
        $fields = 4;
        $field_count = 0;
        $row_count = 0;
        if (!empty($_FILES)) {
            for($x = 0; $x < $number_files; $x++) {
                foreach ($_FILES AS $reg_array) {
                    foreach ($reg_array AS $segment => $value)
                    {
                        if ($field_count == $fields)
                        {
                            $temp_files[$row_count][$segment] = $value[$x];
                            $field_count = 0;
                            $row_count++;
                        }
                        else
                        {
                            $field_count++;
                            $temp_files[$row_count][$segment] = $value[$x];
                        }
                    } // end foreach
                } // end foreach
            } // end for
        } // end if
    
    

    【讨论】:

      猜你喜欢
      • 2019-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-08
      • 1970-01-01
      • 1970-01-01
      • 2015-05-02
      相关资源
      最近更新 更多