programing

PHP - 여러 파일 업로드

telebox 2023. 9. 14. 22:30
반응형

PHP - 여러 파일 업로드

워드프레스용 플러그인 작업을 하고 있는데 양식에서 여러 장의 사진을 업로드 할 수 있으면 좋겠습니다.지금 사진 두 장에 대한 양식을 가지고 있고 그것을 비워 제출하면 $_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 
        ) 
    ) 
)

이제 문제는 워드프레스의 업로드 핸들러인 wp_handle_upload를 사용하고 싶다는 것입니다.$_FILES 배열을 인수로 예상하지만 파일이 하나뿐입니다.제 것처럼 세 개가 아니라 두 개의 배열밖에 안 되는 것 같아요.그래서 $_FILES 배열에서 파일을 한 번에 하나씩 제출하는 방법이 있는지 궁금합니다.각 배열에 동일한 키가 있는 파일입니다.

편집: wp_handle_upload에서 $_FILES 배열을 인수로 원하는 것을 알고 게시물을 변경했습니다.

시도:

$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);
  }
}

나는 이것을 시도해 보았는데 효과가 있었습니다.

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;
    }
}

파일 배열을 순환한 다음 uppload_handler를 호출할 수 없습니까?

예.

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] );
}

언급URL : https://stackoverflow.com/questions/4178873/php-uploading-multiple-files

반응형