$ file ['tmp_name'] существует в массиве, но в l oop нет? - PullRequest
0 голосов
/ 06 апреля 2020

Я хочу загрузить изображение в раздел WordPress Media с этим кодом:

            require( dirname(__FILE__) . '/../../../wp-load.php' );
            $wordpress_upload_dir = wp_upload_dir();
            $j = 1;
            $new_file_path = $wordpress_upload_dir['path'] . '/' . $array['name'];
            $new_file_mime = mime_content_type( $array['tmp_name'] );
            if( empty( $array) )
                die( 'File is not selected.' );
            if( $image['error'] )
                die( $array['error'] );
            if( $image['size'] > wp_max_upload_size() )
                die( 'It is too large than expected.' );
            // if( !in_array( $new_file_mime, get_allowed_mime_types() ) )
            //     die( 'WordPress doesn\'t allow this type of uploads.' );
            while( file_exists( $new_file_path ) ) {
                $j++;
                $new_file_path = $wordpress_upload_dir['path'] . '/' . $j . '_' . $array['name'];
            }
            if( move_uploaded_file( $array['tmp_name'], $new_file_path ) ) {


                $upload_id = wp_insert_attachment( array(
                    'guid'           => $new_file_path, 
                    'post_mime_type' => $new_file_mime,
                    'post_title'     => preg_replace( '/\.[^.]+$/', '', $array['name'] ),
                    'post_content'   => '',
                    'post_status'    => 'inherit'
                ), $new_file_path );

                // wp_generate_attachment_metadata() won't work if you do not include this file
                require_once( ABSPATH . 'wp-admin/includes/image.php' );

                // Generate and save the attachment metas into the database
                $attachment_id = wp_update_attachment_metadata( $upload_id, wp_generate_attachment_metadata( $upload_id, $new_file_path ) );
            }

Проблема этого сценария в том, что этот сценарий не может загружать несколько изображений из массива, откуда я пришел к идее для зацикливания каждого отдельного изображения в массиве:

Полный код:

$estate_gallery = $_FILES['gallery']; // here is array of images from form 

Я хочу l oop любое изображение как одно и попытаться загрузить его на WordPress. Код вниз:

foreach( $estate_gallery as $image ) {
            require( dirname(__FILE__) . '/../../../wp-load.php' );
            $wordpress_upload_dir = wp_upload_dir();
            $j = 1;
            $new_file_path = $wordpress_upload_dir['path'] . '/' . $image['name'];
            $new_file_mime = mime_content_type( $image['tmp_name'] );
            if( empty( $image ) )
                die( 'File is not selected.' );
            if( $image['error'] )
                die( $image['error'] );
            if( $image['size'] > wp_max_upload_size() )
                die( 'It is too large than expected.' );
            // if( !in_array( $new_file_mime, get_allowed_mime_types() ) )
            //     die( 'WordPress doesn\'t allow this type of uploads.' );
            while( file_exists( $new_file_path ) ) {
                $j++;
                $new_file_path = $wordpress_upload_dir['path'] . '/' . $j . '_' . $image['name'];
            }
            if( move_uploaded_file( $image['tmp_name'], $new_file_path ) ) {


                $upload_id = wp_insert_attachment( array(
                    'guid'           => $new_file_path, 
                    'post_mime_type' => $new_file_mime,
                    'post_title'     => preg_replace( '/\.[^.]+$/', '', $image['name'] ),
                    'post_content'   => '',
                    'post_status'    => 'inherit'
                ), $new_file_path );

                // wp_generate_attachment_metadata() won't work if you do not include this file
                require_once( ABSPATH . 'wp-admin/includes/image.php' );

                // Generate and save the attachment metas into the database
                $attachment_id = wp_update_attachment_metadata( $upload_id, wp_generate_attachment_metadata( $upload_id, $new_file_path ) );
            }
    }

В результате я получаю это предупреждение: Кстати. Изображения не загружены!

Warning: mime_content_type(): Can only process string or stream arguments 

Поскольку $image['tmp_name'] возвращает NULL!

...