Weird format of $_FILES array when having multiple fields

匿名 (未验证) 提交于 2019-12-03 01:28:01

问题:

I have a form that has some fields like this:

I would expect i would do something like this:

Array (     [images] => Array         (             [0] => Array                 (                     [name] => test.jpg                     [type] => image/jpeg                     [tmp_name] => /tmp/nsl54Gs                     [error] => 0                     [size] => 1715                 )              [1] => Array                 (                     [name] => test.jpg                     [type] => image/jpeg                     [tmp_name] => /tmp/nsl54Gs                     [error] => 0                     [size] => 1715                 )              [2] => Array                 (                     [name] => test.jpg                     [type] => image/jpeg                     [tmp_name] => /tmp/nsl54Gs                     [error] => 0                     [size] => 1715                 )              [3] => Array                 (                     [name] => test.jpg                     [type] => image/jpeg                     [tmp_name] => /tmp/nsl54Gs                     [error] => 0                     [size] => 1715                 )         ) ) 

But i get something like this:

Array (     [images] => Array         (             [name] => Array                 (                     [0] => test.jpg                     [1] => test.jpg                     [2] => test.jpg                     [3] => test.jpg                 )              [type] => Array                 (                     [0] => image/jpeg                     [1] => image/jpeg                     [2] => image/jpeg                     [3] => image/jpeg                 )              [tmp_name] => Array                 (                     [0] => /tmp/nsl54Gs                     [1] => /tmp/nsl54Gs                     [2] => /tmp/nsl54Gs                     [3] => /tmp/nsl54Gs                 )              [error] => Array                 (                     [0] => 0                     [1] => 0                     [2] => 0                     [3] => 0                 )              [size] => Array                 (                     [0] => 1715                     [1] => 1715                     [2] => 1715                     [3] => 1715                 )          )  ) 

How do I get the array in the form I expect?

回答1:

This is completely normal format, it always has been like that. If you want a different structure, you can transform it in your application, like this:

 $_FILES['images']['name'][$i],         'type' => $_FILES['images']['type'][$i],         'tmp_name' => $_FILES['images']['tmp_name'][$i],         'error' => $_FILES['images']['error'][$i],         'size' => $_FILES['images']['size'][$i]     ); }  ?> 

Good luck!



回答2:

You don't need to, you can easily work with this structure:

foreach ($_FILES['images']['name'] as $key => $name) {     $type = $_FILES['images']['type'][$key];     $tmp_name = $_FILES['images']['tmp_name'][$key];     $error = $_FILES['images']['error'][$key];     $size = $_FILES['images']['size'][$key];      // Do your stuff } 


回答3:

Here is a function that does that and works with name attributes as:

name="artist[images][]" name="images[]" name="image" 

Also works with more than one file field:

PHP:

function get_files( $array )  {     $output = array();     foreach ( $array as $base_key => $file ) {         if ( is_array($file['name']) ) {             $file_keys = array_keys( $file['name'] );             foreach ( $file_keys as $file_key ) {                 if ( is_array( $file['name'][$file_key] ) ) {                     $keys = array_keys( $file['name'][$file_key] );                     foreach ( $keys as $key ) {                         $output[$base_key][$file_key][$key] = array (                              'name' => $file['name'][$file_key][$key],                              'type' => $file['type'][$file_key][$key],                              'tmp_name' => $file['tmp_name'][$file_key][$key],                              'error' => $file['error'][$file_key][$key],                              'size' => $file['size'][$file_key][$key]                          );                     }                 } else $output[$base_key][$file_key] = array(                      'name' => $file['name'][$file_key],                      'type' => $file['type'][$file_key],                      'tmp_name' => $file['tmp_name'][$file_key],                      'error' => $file['error'][$file_key],                      'size' => $file['size'][$file_key]                      );              }         } else $output[$base_key] = $file;     }     return $output; } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!