PHP -上传多个文件

yks3o0rb  于 2022-12-02  发布在  PHP
关注(0)|答案(3)|浏览(139)

我正在为wordpress开发一个插件,我希望能够从一个表单上传多张图片。现在,当我有一个包含两张图片的表单并提交它时,我的$_FILES数组看起来像这样:

Array (
    [image] => Array ( 
        [name] => Array ( 
            [1] => 
            [2] =>
        ) 
        [type] => Array ( 
            [1] => 
            [2] =>
        ) 
        [tmp_name] => Array ( 
            [1] => 
            [2] =>
        ) 
        [error] => Array ( 
            [1] => 4 
            [2] => 4
        ) 
        [size] => Array ( 
            [1] => 0 
            [2] => 0 
        ) 
    ) 
)

现在的问题是,我想使用wordpress的上传处理程序wp_handle_upload。它需要$_FILES数组作为参数,但只能有一个文件。我猜它只能有两个数组深,而不是我的三个。所以我想知道是否有一种方法可以从$_FILES数组中一次提交一个文件。每个数组中的文件都有相同的键。
编辑:自从我了解到wp_handle_upload需要$_FILES数组作为参数后,就修改了帖子。

e4eetjau

e4eetjau1#

试试看:

$files = $_FILES['image'];
foreach ($files['name'] as $key => $value) {
  if ($files['name'][$key]) {
    $file = array(
      'name'     => $files['name'][$key],
      'type'     => $files['type'][$key],
      'tmp_name' => $files['tmp_name'][$key],
      'error'    => $files['error'][$key],
      'size'     => $files['size'][$key]
    );
    wp_handle_upload($file);
  }
}
lymgl2op

lymgl2op2#

难道不能循环遍历文件数组,然后调用upload_handlers吗?
例如:

for( $i = 1; $i <= count( $_FILES['image']['name']; $i++ ) {
    // just cguessing on the args wp_handle_upload takes
    wp_handle_upload( $_FILES['images']['tmp'][$i], $_FILES['images']['name'][$i] );
}
x9ybnkn6

x9ybnkn63#

我试过这个,对我很有效

foreach ($course_video_files['name'] as $key => $value):
                if ($course_video_files['name'][$key]) {
                    $file = array(
                        'name'     => $course_video_files['name'][$key],
                        'type'     => $course_video_files['type'][$key],
                        'tmp_name' => $course_video_files['tmp_name'][$key],
                        'error'    => $course_video_files['error'][$key],
                        'size'     => $course_video_files['size'][$key]
                    );

                    $upload_video_id = MPR_Core::upload_media($file);
                    if( is_int($upload_video_id) ):
                      // if upload is success do something with attachment id
                   
                    endif;
                }
 endforeach;

并使用此功能上传

function upload_media($file, $post_id = null){
    // handle video upload
    $uploadedfile = $file;
    $upload_overrides = array(
        'test_form' => false
    );

    $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
    if ( $movefile && ! isset( $movefile['error'] ) ) {

        if( is_array($movefile) && !empty($movefile) ):

            // $filename should be the path to a file in the upload directory.
            $filename = $movefile['file'];
            //$filename = '/path/to/uploads/2013/03/filename.jpg';

            // The ID of the post this attachment is for.
            $parent_post_id = $post_id;

            // Check the type of file. We'll use this as the 'post_mime_type'.
            $filetype = wp_check_filetype( basename( $filename ), null );

            // Get the path to the upload directory.
            $wp_upload_dir = wp_upload_dir();

            // Prepare an array of post data for the attachment.
            $attachment = array(
                'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ),
                'post_mime_type' => $filetype['type'],
                'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
                'post_content'   => '',
                'post_status'    => 'inherit'
            );

            // Insert the attachment.
            $attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );

            // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
            require_once( ABSPATH . 'wp-admin/includes/image.php' );

            // Generate the metadata for the attachment, and update the database record.
            $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
            wp_update_attachment_metadata( $attach_id, $attach_data );

            set_post_thumbnail( $parent_post_id, $attach_id );

            return $attach_id;
        else:
            return false;
        endif;

    } else {
        /*
         * Error generated by _wp_handle_upload()
         * @see _wp_handle_upload() in wp-admin/includes/file.php
         */
        //echo $movefile['error'];
        return $movefile;
    }
}

相关问题