从AJAX提交中拆分数组$ _FILES

时间:2016-04-30 10:57:48

标签: javascript php jquery arrays ajax

我的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
    )

)

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

First Array

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
    )

)

2 个答案:

答案 0 :(得分:1)

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

$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);

答案 1 :(得分:1)

您可以利用type juggling

$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 );