在同一表单中提交两个文件输入字段

时间:2021-08-28 作者:yahya Elgoud

我有一个表单,它有两个文件输入字段,每个输入都应该在自定义帖子类型的不同元框中提交,当我尝试提交表单时,只会使用第一个,而忽略第二个!

这是我的表格

<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>
这里是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);
            }
        }
    }
}

1 个回复
SO网友:Pat J

以下是我认为正在发生的事情。我已经在代码的相关部分添加了注释。

// 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\'])) {
// ...
}
我建议使用不同的变量名,这样就不会覆盖$_FILES.

例如:替换

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

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