【问题标题】:PHP $_FILES not using all file inputsPHP $_FILES 不使用所有文件输入
【发布时间】:2018-08-31 17:52:25
【问题描述】:

我不得不修补 PHP 邮件解析器。问题是,$_FILES 变量只管理第一个文件输入。设置方式,输入如下所示: <input type="file" name="form.steps.0.field.4" />。 问题是,下一个输入是与名称递增的输入相同,如下所示:<input type="file" name="form.steps.0.field.5" />

循环$_FILES时,这是当前的逻辑:

foreach($_FILES as $input) {
    ...
}

在这种情况下,$_FILES 只有<input type="file" name="form.steps.0.field.4" /> 的文件,而第二个文件输入被视为文本,其值为C:\\fakepath\\{filename}

如果不使用像<input type="file" name="files[]" /> 这样的单一输入名称,我该如何实现呢?

编辑:

$_FILES 中的var_dump 产生以下结果:

array(1) {
    ["files"]=>
        array(5) {
            ["name"]=>
                array(1) {
                    [0]=>
                    string(22) "Checklist-template.pdf"
                }
            ["type"]=>
                array(1) {
                    [0]=>
                    string(15) "application/pdf"
                }
            ["tmp_name"]=>
                array(1) {
                    [0]=>
                    string(14) "/tmp/phpZxkSFz"
                }
            ["error"]=>
                array(1) {
                    [0]=>
                    int(0)
                }
            ["size"]=>
                array(1) {
                    [0]=>
                    int(115033)
            }
        }
    }

编辑#2

添加动作(用于前端和后端):

add_action('wp_ajax_sp_form_submit', 'spForm_submit', 10, 1);
add_action('wp_ajax_nopriv_sp_form_submit', 'spForm_submit', 10, 1);

执行数据的 PHP :

function spForm_submit(){
    if(!(is_array($_POST) && defined('DOING_AJAX') && DOING_AJAX)){
        return;
    }
    var_dump( $_FILES );
    die();
}

带有 AJAX 调用的 JS:

$('.sp-form .btn-submit').unbind('click').bind('click', function(e){
    e.preventDefault();
    e.stopPropagation();

    $('input[type="hidden"]', form).each(function(){
        data[$(this).attr('name')] = $(this).val();
    });

    var fd = new FormData(),
        file_inputs = $('input[type="file"]', form);

    $.each($(file_inputs), function(i, obj) {
        $.each(obj.files,function(j,file){
            fd.append('files[' + j + ']', file);
        });
    });

    function appendFormdata(FormData, data, name, cback){
        name = name || '';
        if (typeof data === 'object'){
            $.each(data, function(index, value){
                if (name === ''){
                    appendFormdata(FormData, value, index, cback);
                } else {
                    appendFormdata(FormData, value, name + '['+index+']', cback);
                }
            });
        } else {
            FormData.append(name, data);
        }
    }

    appendFormdata(fd, {form: data}, null);
    fd.append( 'action', 'sp_form_submit' );

    $.ajax({
        url         : window.forms_cback, // AJAX URL
        type        : "POST",

        data        : fd,
        contentType : false,
        processData : false,
        dataType    : "json",
        success     : function(response) {
            // removed for the purpose of the post
        }
    });
});

还有$_FILES 数组的输出:

array(1) {
    ["files"]=>
        array(5) {
            ["name"]=>
                array(1) {
                    [0]=>
                    string(22) "Checklist-template.pdf"
                }
            ["type"]=>
                array(1) {
                    [0]=>
                    string(15) "application/pdf"
                }
            ["tmp_name"]=>
                array(1) {
                    [0]=>
                    string(14) "/tmp/phpZxkSFz"
                }
            ["error"]=>
                array(1) {
                    [0]=>
                    int(0)
                }
            ["size"]=>
                array(1) {
                    [0]=>
                    int(115033)
            }
        }
    }

【问题讨论】:

  • 它应该有所有的文件输入。你能发帖var_dump($_FILES)
  • @Barmar 我已将 var_dump 添加到问题中
  • 这是<input type="file" name="files[]"> 的转储而不是<input type="file" name="form.steps.0.field.4">
  • 看看this第一个答案你会看到我相信它会有所帮助。
  • 你有一个错字。在fd.append('files[' + j + ']', file); 中,变量应该是i,而不是j

标签: php ajax wordpress file


【解决方案1】:

正如 Barmar 所指出的,我的问题不在于我的 $_FILES 数组,而在于我的 FileData 操作中的拼写错误。

Quote :

你有一个错字。在fd.append('files[' + j + ']', file); 中,变量应该是i,而不是j

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 2013-05-09
    • 2012-02-04
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    • 2017-05-23
    相关资源
    最近更新 更多