使用 FormData 后使用 $_FILES 从多个文件输入访问多个文件(并添加到 WP 后端的 ACF 数据)

Access multiple files from multiple file inputs using $_FILES after using FormData (And add to ACF data on WP Backend)

使用 Wordpress,我尝试使用 $_FILES 从两个文件输入之一访问文件,但 运行 遇到一些问题:

概述 - 我正在使用具有两个文件字段的前端表单,这两个字段都接受多个文件:

<form id="form" action="" method="post">
    <input type="text" name="my_name">
    <input type="file" name="reference_images[]" multiple>
    <input type="file" name="photo_of_area[]" multiple>
</form>

文件输入将拍摄图像,我需要将图像从“reference_images”上传到一个转发器字段,将“photo_of_area”上传到另一个转发器字段。此表单是使用 FormData 通过 AJAX 编辑的 - 其功能如下:

function saveForm(varform) {
    blockUI();
    jQuery.ajax({
        type: "POST",
        url: window.ajaxObject.ajaxUrl,
        dataType: "JSON",
        data: varform,
        processData: false,
        contentType: false,
        cache: false,
        success: onSuccesPing(),
        crossDomain:true,
    });
}

jQuery('#form').submit(function(event) {
    event.preventDefault(); // Prevent form submitting as normal
    var formData = new FormData(jQuery('#form')[0]);
    formData.append("action", "messaging_post");       
    saveForm(formData);
});

然后我有一个函数处理下面的 messaging_post 操作,这会创建一个新的 post 和表单数据,并循环附加的图像并将它们注入我的 ACF 转发器:

add_action( 'wp_ajax_messaging_post', 'messaging_post' );
add_action('wp_ajax_nopriv_messaging_post', 'messaging_post');
function messaging_post(){
    if(isset($_POST['my_name'])) {
        $name = sanitize_text_field($_POST['my_name']);
        $new_post = array(
            'ID' => '',
            'post_type' => 'quote_requests',
            'post_status' => 'publish',
            'post_title' => $title, 
            'post_content' => '',
        );        
        
        $post_id = wp_insert_post($new_post);
        $post = get_post($post_id);

        update_field('name', $name, $post_id); // Updates the ACF text field 'name' for the inserted post


        // Works, but uploads files from BOTH file inputs to the single ACF repeater:
        foreach($_FILES as $value){
            for ($i=0; $i <count($value['name']); $i++) { 
                $errors= array();
                $file_name = $value['name'][$i];
                $file_size = $value['size'][$i];
                $file_tmp = $value['tmp_name'][$i];
                $file_type = $value['type'][$i];
                $file_ext=strtolower(end(explode('.',$value['name'][$i])));
                
                if(empty($errors)==true) {
                    $wordpress_upload_dir = wp_upload_dir();
                    $profilepicture = $wordpress_upload_dir['path'].'/';
                    move_uploaded_file($file_tmp, $profilepicture.$file_name);
                } else{
                    print_r($errors);
                }
                $file_name_and_location = $profilepicture.$file_name;
                $file_title_for_media_library = $value['name'][$i];
                $fildename = $value['name'][$i];
                $arr_file_type     = wp_check_filetype(basename($fildename));
                $uploaded_file_type = $arr_file_type['type'];
                $attachment = array(
                    'post_mime_type' => $uploaded_file_type,
                    'post_title' => addslashes($file_title_for_media_library),
                    'post_content' =>  $fildename,
                    'post_status' => 'inherit',
                    'post_parent' =>  0,
                    'post_author' => get_current_user_id(),
                );        
                wp_read_image_metadata( $file_name_and_location );
                $attach_id     = wp_insert_attachment( $attachment, $file_name_and_location,true,false);         
                $attach_data = wp_generate_attachment_metadata($attach_id,$file_name_and_location );
                wp_update_attachment_metadata( $attach_id, $attach_data );
                $images[]= array("image" => $attach_id);
            }
        }
        update_field('photo_area', $images, $post_id);
    }
}

以上有效,并使用表单中的名称填充创建的 post,但这会将 reference_images 和 photo_of_area 中的文件附加到 photo_area ACF中继器。

当尝试使用如下函数访问 $_FILES 时:

foreach($_FILES["photo_of_area"] as $value){
    for ($i=0; $i <count($value['name']); $i++) { 
        $errors= array();
        $file_name = $value['name'][$i];
        $file_size = $value['size'][$i];
        $file_tmp = $value['tmp_name'][$i];
        $file_type = $value['type'][$i];
        $file_ext=strtolower(end(explode('.',$value['name'][$i])));
        
        if(empty($errors)==true) {
            $wordpress_upload_dir = wp_upload_dir();
            $profilepicture = $wordpress_upload_dir['path'].'/';
            move_uploaded_file($file_tmp, $profilepicture.$file_name);
        } else{
            print_r($errors);
        }
        $file_name_and_location = $profilepicture.$file_name;
        $file_title_for_media_library = $value['name'][$i];
        $fildename = $value['name'][$i];
        $arr_file_type     = wp_check_filetype(basename($fildename));
        $uploaded_file_type = $arr_file_type['type'];
        $attachment = array(
            'post_mime_type' => $uploaded_file_type,
            'post_title' => addslashes($file_title_for_media_library),
            'post_content' =>  $fildename,
            'post_status' => 'inherit',
            'post_parent' =>  0,
            'post_author' => get_current_user_id(),
        );        
        wp_read_image_metadata( $file_name_and_location );
        $attach_id     = wp_insert_attachment( $attachment, $file_name_and_location,true,false);         
        $attach_data = wp_generate_attachment_metadata($attach_id,$file_name_and_location );
        wp_update_attachment_metadata( $attach_id, $attach_data );
        $images[]= array("image" => $attach_id);
    }
}
update_field('photo_area', $images, $post);

这似乎不起作用,而且 returns 什么都没有。

我假设在通过 FormData() 之后,这些文件现在无法通过普通的 $_FILES['name_of_input'] 访问,应该对它们做些其他的事情?

我也试过将图像附加到 FormData,但似乎遇到了同样的问题。

任何人都可以阐明我如何在通过后相互独立地访问 $_FILES["photo_of_area"] 和 $_FILES["reference_images"]通过 FormData()?或者我应该考虑实现所需行为的任​​何替代方法。

理想情况下,我需要分别访问每个文件输入的图像。

谢谢!

我通过改变 $_FILES 数组的循环实现了这个:

$photo_of_area = $_FILES["photo_of_area"]; 
foreach ($photo_of_area['name'] as $key => $value) {
    $errors     = array();

    $file_name  = $photo_of_area['name'][$key];
    $file_size  = $photo_of_area['size'][$key];
    $file_tmp   = $photo_of_area['tmp_name'][$key];
    $file_type  = $photo_of_area['type'][$key];
    $file_ext   = strtolower(end(explode('.',$photo_of_area['name'][$key])));


    if (empty($errors) == true) {
        $wordpress_upload_dir = wp_upload_dir();
        $upload_dir_path = $wordpress_upload_dir['path'].'/';
        move_uploaded_file($file_tmp, $upload_dir_path.$file_name);
    } else {
        print_r($errors);
    }


    $file_name_and_location         = $upload_dir_path.$file_name;
    $file_title_for_media_library   = $photo_of_area['name'][$key];
    $fildename                      = $photo_of_area['name'][$key];
    $arr_file_type                  = wp_check_filetype(basename($fildename));
    $uploaded_file_type             = $arr_file_type['type'];


    $attachment = array(
        'post_mime_type'    => $uploaded_file_type,
        'post_title'        => addslashes($file_title_for_media_library),
        'post_content'      =>  $fildename,
        'post_status'       => 'inherit',
        'post_parent'       =>  0,
        'post_author'       => get_current_user_id(),
    );    

    wp_read_image_metadata( $file_name_and_location );
    $attach_id          = wp_insert_attachment( $attachment, $file_name_and_location,true,false);         
    $attach_data        = wp_generate_attachment_metadata($attach_id,$file_name_and_location );
    wp_update_attachment_metadata( $attach_id, $attach_data );
    $area_photos_array[]           = array("image" => $attach_id);
}

update_field('photo_area', $area_photos_array, $post_id); 

这成功地将上传的每张图像注入到 $post_id 指定的当前 post 中 photo_area 转发器的新行。