上传具有多个名称的文件输入的问题

时间:2018-05-22 10:10:15

标签: php file-upload

我有几个文件输入字段,每个文件输入都有一个通用名称和唯一名称。唯一名称用于某些验证目的。

<input name="file12 ftr_file_uploads[]" class="multi_files file " type="file">
<input name="file10 ftr_file_uploads[]" class="multi_files file " type="file">
<input name="file10 ftr_file_uploads[]" class="multi_files file " type="file">
...............

尝试以PHP格式提交上传文件时, $ _FILES的内容如下。

    array(2) { 
   ["file1_ftr_file_uploads"]=> array(5)
        { ["name"]=> array(1) { [0]=> string(13) "Jellyfish.jpg" } 
        ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } 
        ["tmp_name"]=> array(1) { [0]=> string(14) "/tmp/phpx7iId2" } 
        ["error"]=> array(1) { [0]=> int(0) } 
        ["size"]=> array(1) { [0]=> int(775702) } }
  ["file2_ftr_file_uploads"]=> array(5) 
        { ["name"]=> array(1) { [0]=> string(12) "Penguins.jpg" } 
        ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } 
        ["tmp_name"]=> array(1) { [0]=> string(14) "/tmp/phpN6QWoD" } 
        ["error"]=> array(1) { [0]=> int(0) } 
        ["size"]=> array(1) { [0]=> int(777835) } } 
  }

数组键名更改为文件输入字段的连接名称。我需要名称为ftr_file_uploads而不是fileIDnumber_ftr_file_uploads

我做了类似的事情。

foreach($_FILES as $keyval=>$value)
    {
          $_FILES['ftr_file_uploads'] = $_FILES[$keyval]; //removed 
           $_FILES['ftr_file_uploads'][] mentioned in the answer 
           unset($_FILES[$keyval]);
    }

当我这样使用时,我得到的结果就是这样。

    array(1) {
  ["ftr_file_uploads"]=>
  array(5) {
    ["name"]=>
    array(1) {
      [0]=>
      string(14) "Lighthouse.jpg"
    }
    ["type"]=>
    array(1) {
      [0]=>
      string(10) "image/jpeg"
    }
    ["tmp_name"]=>
    array(1) {
      [0]=>
      string(14) "/tmp/phpLdslxb"
    }
    ["error"]=>
    array(1) {
      [0]=>
      int(0)
    }
    ["size"]=>
    array(1) {
      [0]=>
      int(561276)
    }
  }
}

我需要这样的结果。

 array(1) {
  ["ftr_file_uploads"]=>
  array(5) {
    ["name"]=>
    array(2) {
      [0]=>
      string(14) "Hydrangeas.jpg"
      [1]=>
      string(5) "w.jpg"
    }
    ["type"]=>
    array(2) {
      [0]=>
      string(10) "image/jpeg"
      [1]=>
      string(10) "image/jpeg"
    }
    ["tmp_name"]=>
    array(2) {
      [0]=>
      string(14) "/tmp/phpKMwmH1"
      [1]=>
      string(14) "/tmp/phpwwHU9G"
    }
    ["error"]=>
    array(2) {
      [0]=>
      int(0)
      [1]=>
      int(0)
    }
    ["size"]=>
    array(2) {
      [0]=>
      int(595284)
      [1]=>
      int(879394)
    }
  }
}

1 个答案:

答案 0 :(得分:1)

foreach ($_FILES as $name => $file) {
    $_FILES['ftr_file_uploads'][] = $file;  // [] means add $file to $_FILES['ftr_file_uploads'] array
    unset($_FILES[$name]); // remove element from $_FILES
}