In a front end form I have a 'multiple' input field for uploading and attaching images to a post and a processing script (from here). I expect that will be uploaded and attached all files that I have selected, but this doesn't happens. Is uploaded and attached only the last selected file (in alphabetical order). When I check the $_FILES array with var_dump($_FILES) I see only one file there. How can I upload and attach to a post multiple files at once?

<?php ob_start(); ?>

<form id="frontpost" method="post" enctype="multipart/form-data">
    <input type="file" multiple name="thumbnail" id="thumbnail">

<?php return ob_get_clean(); }

and a processing script:

if (!function_exists('wp_generate_attachment_metadata')) {
    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
    require_once(ABSPATH . "wp-admin" . '/includes/media.php');

if ($_FILES) {
    foreach ($_FILES as $file => $array) {
        if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
            return "upload error : " . $_FILES[$file]['error'];
        media_handle_upload( $file, $post_id );

closed as off-topic by Mark Kaplun, TheDeadMedic, Howdy_McGee, s_ha_dum Dec 24 '15 at 22:36

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • If files are not being sent to the server then it is an HTML question, not a wordpress one.... – Mark Kaplun Dec 23 '15 at 7:01

You'll need to add brackets to the name of your multiple input form element for PHP to interpret it as an array.

<input type="file" multiple name="thumbnail[]" id="thumbnail">  

You'll then get an array named 'thumbnail' with 5 arrays within it. See this comment on php.net for a breakdown of the structure.

Edit: Notice the difference in what is returned for a single vs a multiple file input. Here's an example with two form fields, one a single file upload and the other a multiple.

<?php if ($_FILES) {

<form id="frontpost" method="post" enctype="multipart/form-data">
    <input type="file" name="single">
    <input type="file" multiple name="multiple[]">
    <button type="submit">Submit</button>

Which prints:

[single] => Array
        [name] => foo.txt
        [type] => text/html
        [tmp_name] => /path/to/tmp/php/qwqerqrq
        [error] => 0
        [size] => 1621

[multiple] => Array
        [name] => Array
                [0] => bar.txt
                [1] => baz.txt

        [type] => Array
                [0] => text/plain
                [1] => text/plain

        [tmp_name] => Array
                [0] => /path/to/tmp/php/vwvwrvwrv
                [1] => /path/to/tmp/php/wqerverhw

        [error] => Array
                [0] => 0
                [1] => 0

        [size] => Array
                [0] => 36976
                [1] => 58355


If your code is setup expecting the structure of a single file in $_FILES, you'll need to update it to work with the very different structure of a multi-file upload.

  • After adding brackets to the input element name my form doesn't work at all (no errors), just reloads after submitting. – Iurie Malai Dec 23 '15 at 6:50
  • Try another var_dump (or post your entire form). It comes through in an odd (to me) format. Also see that comment on php.net for a workaround if that's the issue. – Will Dec 23 '15 at 6:54
  • Sorry! I discovered a notice message: Array to string conversion in /.../submit-from-front.php on line 167. This line is the return "upload error : " . $_FILES[$file]['error'];. – Iurie Malai Dec 23 '15 at 6:56
  • Checked with var_dump, now the $_FILES array include two files, but the mentioned 167 line also returns an error. I will check this later, now I must to go. – Iurie Malai Dec 23 '15 at 7:05
  • Yeah, sounds like what I'm describing. foreach ($_FILES as $file => $array) {... will not work with the multiple submit because the format of the POSTed object has changed. Inspect that var_dump and make sure you're looping over the correct array.. – Will Dec 23 '15 at 7:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.