如果你尝试使用多维名称上传文件,例如:
<input type="file" name="submission[screenshot]" />
<input type="file" name="other[dem][][img][]" />
你会得到一个意想不到的格式,例如:
<?php
array(
'submission' => array
(
'name' => array( 'screenshot' => 'monster_wallpaper.jpg' ),
'type' => array( 'screenshot' => 'image/jpeg' ),
'tmp_name' => array( 'screenshot' => '/tmp/php48lX2Y' ),
'error' => array( 'screenshot' => 0 ),
'size' => array( 'screenshot' => 223262 ),
),
....
?>
可以使用以下函数递归地将数组重新格式化为通常的格式
<?php
function format_files_array( $files, $name = null, &$new = false, $path = false ){
$names = array( 'name' => 'name', 'type' => 'type', 'tmp_name' => 'tmp_name', 'error' => 'error', 'size' => 'size' );
foreach( $files as $key => &$part )
{
$key = ( string ) $key;
if( in_array( $key, $names ) )
$name = $key;
if( !in_array( $key, $names ) )
$path[] = $key;
if( is_array( $part ) )
$part = format_files_array( $part, $name, $new, $path );
elseif( !is_array( $part ) )
{
$current =& $new;
foreach( $path as $p )
$current =& $current[$p];
$current[$name] = $part;
unset( $path );
$name = null;
}
}
return $new;
}
?>