在PHP中合并文件和发布数据 [英] Merging File and Post data in PHP
问题描述
在工作中,我一直在处理一些复杂的表单(发布交响曲的页面)图片上传字段.我需要一种将$_FILES
与$_POST
快速合并的方法,不幸的是,您不能简单地将它们与array_merge
合并,因为它们没有遵循相同的结构.
At work I've been dealing with some complex forms (publish pages of Symphony) that contain multiple image upload fields. I need a way to quickly merge $_FILES
with $_POST
, unfortunately you cannot simply merge the two with array_merge
because they don't follow the same structure.
基本上,如果您有$_POST[a][b]
,则为$_FILES[a][*][b]
.将*
替换为name
,type
,tmp_name
,error
或size
之一.
Basically if you have $_POST[a][b]
it would be $_FILES[a][*][b]
. Replace *
with one of name
, type
, tmp_name
, error
or size
.
$_FILES
数组的内容作为标准内容:
The content of the $_FILES
array as standard:
array
'image-a' => array
'name' => string '' (length=0)
'type' => string '' (length=0)
'tmp_name' => string '' (length=0)
'error' => int 4
'size' => int 0
'image-b' => array
'name' => string '' (length=0)
'type' => string '' (length=0)
'tmp_name' => string '' (length=0)
'error' => int 4
'size' => int 0
'image' => array
'name' => array
'sub' => array
'c' => string '' (length=0)
'type' => array
'sub' => array
'c' => string '' (length=0)
'tmp_name' => array
'sub' => array
'c' => string '' (length=0)
'error' => array
'sub' => array
'c' => int 4
'size' => array
'sub' => array
'c' => int 0
以及与$_POST
合并后的所需数组:
And the desired array after merging with $_POST
:
array
'MAX_FILE_SIZE' => string '5242880' (length=7)
'image-a' => array
'name' => string '' (length=0)
'type' => string '' (length=0)
'tmp_name' => string '' (length=0)
'error' => int 4
'size' => int 0
'image-b' => array
'name' => string '' (length=0)
'type' => string '' (length=0)
'tmp_name' => string '' (length=0)
'error' => int 4
'size' => int 0
'image' => array
'sub' => array
'c' => array
'name' => string '' (length=0)
'type' => string '' (length=0)
'tmp_name' => string '' (length=0)
'error' => int 4
'size' => int 0
推荐答案
这是我想出的解决方案,调用get_file_post_data
返回合并的数组:
This is the solution I came up with, calling get_file_post_data
returns the merged array:
<?php
if (!empty($_POST)) {
header('content-type: text/plain');
function merge_file_post_data($type, $file, &$post) {
foreach ($file as $key => $value) {
if (!isset($post[$key])) $post[$key] = array();
if (is_array($value)) merge_file_post_data($type, $value, $post[$key]);
else $post[$key][$type] = $value;
}
}
function get_file_post_data() {
$files = array(
'name' => array(),
'type' => array(),
'tmp_name' => array(),
'error' => array(),
'size' => array()
);
$post = $_POST;
// Flip the first level with the second:
foreach ($_FILES as $key_a => $data_a) {
foreach ($data_a as $key_b => $data_b) {
$files[$key_b][$key_a] = $data_b;
}
}
// Merge and make the first level the deepest level:
foreach ($files as $type => $data) {
merge_file_post_data($type, $data, $post);
}
return $post;
}
var_dump(get_file_post_data());
}
else echo '
<form action="" method="post" enctype="multipart/form-data">
<input name="MAX_FILE_SIZE" type="hidden" value="5242880">
<div><label class="file">Image A <input name="image-a" type="file"></label></div>
<div><label class="file">Image B <input name="image-b" type="file"></label></div>
<div><label class="file">Image C <input name="image[sub][c]" type="file"></label></div>
<div><button type="submit">Send</button></div>
</form>
';
?>
这篇关于在PHP中合并文件和发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!