-1

I'm having to patch a PHP Mail parser. Thing is, the $_FILES variable only manages the first file input. The way things are set, the input looks like this: <input type="file" name="form.steps.0.field.4" />. Thing is, the next input is input is the same where the name is incremented, like so: <input type="file" name="form.steps.0.field.5" />.

When looping the $_FILES, this is the current logic:

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

In this case, the $_FILES only has the file of <input type="file" name="form.steps.0.field.4" />, whereas the second file input is treated as text and it's value is C:\\fakepath\\{filename}.

Without using a single input name like <input type="file" name="files[]" />, how can I achieve this?

Edit:

var_dump of $_FILES produces the following:

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)
            }
        }
    }

Edit #2

Adding action (to be used on front and back end) :

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

The PHP executing the data :

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

The JS with the AJAX Call :

$('.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
        }
    });
});

And the output of the $_FILES array :

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)
            }
        }
    }
davewoodhall
  • 998
  • 3
  • 18
  • 43

1 Answers1

0

As pointed out by Barmar, my issue was not in my $_FILES array but by a typo in my FileData manipulation.

Quote :

You have a typo. In fd.append('files[' + j + ']', file); the variable should be i, not j.

davewoodhall
  • 998
  • 3
  • 18
  • 43