从AJAX提交中拆分数组$_FILES


Split array $_FILES from AJAX submission

在我的html表单中,我有一个公共输入和两个文件输入,如下所示:

div class="col-md-4">
   <div class="form-group">
       <label class="control-label col-md-3">Size</label>
           <div class="col-md-9">
              <?php
                $size = array(
                         "type" => "text",
                         "name" => "size",
                         "id" => "size",
                         "class" => "form-control"
                        );
               echo form_input($size);
               ?>
               <span class="help-block"></span>
            </div>
          </div>
       </div>

<div class="col-md-6">
    <div class="form-group">
        <label class="control-label col-md-2">Image Container:</label>
            <div class="col-md-10">
               <input type="file" multiple="" name="images_container[]" id="file_container" required="required">
               <p class="help-block"></p>
            </div>
    </div>
</div>
 <div class="col-md-6">
     <div class="form-group">
        <label class="control-label col-md-2">Image If Coil Is Damage</label>
             <div class="col-md-10">
                <input type="file" multiple="" name="images[]" id="files" >
                <p class="help-block"></p>
             </div>
     </div>
</div>

我使用ajax jquery 抓取它们

$('#form').submit(function () {
        $('#btnSave').text('saving...'); //change button text
        $('#btnSave').attr('disabled', true); //set button disable
        var url;
        var fd = new FormData();
        var file_data = $('#files')[0].files; // for multiple files
        var file_data_container = $('#file_container')[0].files; // for multiple files
        for (var j = 0; j < file_data_container.length; j++) {
            fd.append("file_container_" + j, file_data_container[j]);
        }
        for (var i = 0; i < file_data.length; i++) {
            fd.append("file_coil" + i, file_data[i]);
        }
        var other_data = $(this).serializeArray();
        $.each(other_data, function (key, input) {
            fd.append(input.name, input.value);
        });
 $.ajax({
            url: url,
            type: "POST",
            data: fd,
            processData: false,
            contentType: false,
            dataType: "JSON",
            success: function (data){}
 });

所以,ajax提交的$_FILES是这样的数组:

Array
(
[file_container_0] => Array
    (
        [name] => bonus.jpg
        [type] => image/jpeg
        [tmp_name] => D:'xampp'tmp'php95B4.tmp
        [error] => 0
        [size] => 616252
    )
[file_container_1] => Array
    (
        [name] => coolas.jpg
        [type] => image/jpeg
        [tmp_name] => D:'xampp'tmp'php9613.tmp
        [error] => 0
        [size] => 641576
    )
[file_container_2] => Array
    (
        [name] => preview.jpg
        [type] => image/jpeg
        [tmp_name] => D:'xampp'tmp'php9633.tmp
        [error] => 0
        [size] => 474285
    )
[file_coil0] => Array
    (
        [name] => 1280x800.jpg
        [type] => image/jpeg
        [tmp_name] => D:'xampp'tmp'php9653.tmp
        [error] => 0
        [size] => 214172
    )
[file_coil1] => Array
    (
        [name] => 1280x960.jpg
        [type] => image/jpeg
        [tmp_name] => D:'xampp'tmp'php9674.tmp
        [error] => 0
        [size] => 261002
    )
)

我的问题是,如何将这个数组拆分为两个数组。

第一阵列

Array
(
[file_container_0] => Array
    (
        [name] => bonus.jpg
        [type] => image/jpeg
        [tmp_name] => D:'xampp'tmp'php95B4.tmp
        [error] => 0
        [size] => 616252
    )
[file_container_1] => Array
    (
        [name] => coolas.jpg
        [type] => image/jpeg
        [tmp_name] => D:'xampp'tmp'php9613.tmp
        [error] => 0
        [size] => 641576
    )
[file_container_2] => Array
    (
        [name] => preview.jpg
        [type] => image/jpeg
        [tmp_name] => D:'xampp'tmp'php9633.tmp
        [error] => 0
        [size] => 474285
    )
)

第二阵列

Array
(
    [file_coil0] => Array
    (
        [name] => 1280x800.jpg
        [type] => image/jpeg
        [tmp_name] => D:'xampp'tmp'php9653.tmp
        [error] => 0
        [size] => 214172
    )
[file_coil1] => Array
    (
        [name] => 1280x960.jpg
        [type] => image/jpeg
        [tmp_name] => D:'xampp'tmp'php9674.tmp
        [error] => 0
        [size] => 261002
    )
)

通过检查索引位置的子字符串,我们可以区分两个数组:

$file_container_array = array();
$file_coil_array = array();
$files_array = $_FILES;
foreach($files_array as $key => $file)
if (strpos($key, 'file_container') !== false) {
    $file_container_array[] = $file;
} else if(strpos($key, 'file_coil') !== false) { 
    $file_coil_array[] = $file;
}
print_r($file_container_array);
print_r($file_coil_array);

您可以利用类型杂耍:

$result = array();
foreach( $_FILES as $key => $val )
{
    $result[ substr($key,0,9)=='file_coil' ][$key] = $val;
}

现在在$result[0]中有所有的file_container值,在$result[1]中有file_coil值。

可能你的数组有可变长度,但如果它有固定长度,你可以使用array_chunk代替:

$result = array_chunk( $_FILES, 3, True );