0

I have a form which has two file input fields, each input should be submitted in different metabox in custom post type, when I try to submit the form it takes only the first one and ignore the second one!

here is my form

<form method="POST" name="vendor_form" enctype="multipart/form-data">
 <div class="mb-3" style="margin-bottom: 20px;">
            <label for="file_metabox" class="form-label">file metabox</label>
            <input class="form-control" type="file" id="file_metabox" name="file_metabox[]" multiple="multiple">
        </div>
        <div class="mb-3" style="margin-bottom: 20px;">
            <label for="file_cert" class="form-label">file Cert</label>
            <input class="form-control" type="file" id="file_cert" name="file_cert[]" multiple="multiple">
        </div>
<button type="submit" class="btn btn-primary" id="submit" name="submit">Submit</button>

and here is the php

if (isset($_FILES['file_metabox'])) {
    $file_metabox = $_FILES['file_metabox'];
    foreach ($file_metabox['name'] as $key => $value) {
        if ($file_metabox['name'][$key]) {
            $file = array(
                'name' => $file_metabox['name'][$key],
                'type' => $file_metabox['type'][$key],
                'tmp_name' => $file_metabox['tmp_name'][$key],
                'error' => $file_metabox['error'][$key],
                'size' => $file_metabox['size'][$key]
            );
            $_FILES = array("file_metabox" => $file);
            foreach ($_FILES as $file => $array) {
                // $newupload = frontend_handle_attachment( $file, $post_success );
                if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) __return_false();

                require_once(ABSPATH . "wp-admin" . '/includes/image.php');
                require_once(ABSPATH . "wp-admin" . '/includes/file.php');
                require_once(ABSPATH . "wp-admin" . '/includes/media.php');

                $attach_id = media_handle_upload($file, $post_success);

                add_post_meta($post_success, 'file_metabox', $attach_id);
            }
        }
    }
   
}
if (isset($_FILES['file_cert'])) {
    $file_cert = $_FILES['file_cert'];
    foreach ($file_cert['name'] as $key => $value) {
        if ($file_cert['name'][$key]) {
            $file = array(
                'name' => $file_cert['name'][$key],
                'type' => $file_cert['type'][$key],
                'tmp_name' => $file_cert['tmp_name'][$key],
                'error' => $file_cert['error'][$key],
                'size' => $file_cert['size'][$key]
            );
            $_FILES = array("file_cert" => $file);
            foreach ($_FILES as $file => $array) {
                // $newupload = frontend_handle_attachment( $file, $post_success );
                if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) __return_false();

                require_once(ABSPATH . "wp-admin" . '/includes/image.php');
                require_once(ABSPATH . "wp-admin" . '/includes/file.php');
                require_once(ABSPATH . "wp-admin" . '/includes/media.php');

                $attach_id = media_handle_upload($file, $post_success);

                add_post_meta($post_success, 'file_cert', $attach_id);
            }
        }
    }
}
5
  • You're overwriting the $_FILES array in your first if() statement: $_FILES = array("file_metabox" => $file);, so even if there was anything in the $_FILES['file_cert'] variable, it's gone by the time your code checks it.
    – Pat J
    Commented Aug 28, 2021 at 1:57
  • thanks for the reply, do you hve any ideas on how to fix this. thanks again Commented Aug 28, 2021 at 2:08
  • Use a different variable name than $_FILES in your code. Maybe something like $_files is what you're looking for (since PHP is case-sensitive.
    – Pat J
    Commented Aug 28, 2021 at 2:51
  • That didnt work! As it using $_FILES array not just a random variable Commented Aug 28, 2021 at 9:38
  • See my answer, since I think this issue is a bit involved to explain in a comment.
    – Pat J
    Commented Aug 28, 2021 at 16:31

1 Answer 1

0

Here's what I think is happening. I've added comments to the relevant sections of your code.

// You use the $_FILES superglobal to get the uploaded files.
if (isset($_FILES['file_metabox'])) {
    $file_metabox = $_FILES['file_metabox'];
    foreach ($file_metabox['name'] as $key => $value) {
        if ($file_metabox['name'][$key]) {
            // ...
            // Here you **overwrite** the $_FILES superglobal.
            $_FILES = array("file_metabox" => $file);
            //...           
        }
    }
   
}

// So here, $_FILES['file_cert'] **no longer exists**.
if (isset($_FILES['file_cert'])) {
// ...
}

I would recommend using a different variable name, so that you're not overwriting the content of $_FILES.

For example: replace

$_FILES = array("file_metabox" => $file);

with

$my_files = array("file_metabox" => $file);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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