admin管理员组

文章数量:1410730

I am using a custom form to upload image and trying to move the file in the Media Library. The wp_insert_attachment() is working fine because I get the attachment id; however I don't see the image that I uploaded in the media library. Following is the code -

            $image_url = $_FILES['input_14']['name'];

            $upload_dir = wp_upload_dir();

            $image_data = file_get_contents( $image_url );

            $filename = basename( $image_url );

            if ( wp_mkdir_p( $upload_dir['path'] ) ) {
              $file = $upload_dir['path'] . '/' . $filename;
            }
            else {
              $file = $upload_dir['basedir'] . '/' . $filename;
            }

            file_put_contents( $file, $image_data );

            $wp_filetype = wp_check_filetype( $filename, null );

            $attachment = array(
              'post_mime_type' => $wp_filetype['type'],
              'post_title' => sanitize_file_name( $filename ),
              'post_content' => '',
              'post_status' => 'inherit'
            );

            $attachment_id = wp_insert_attachment( $attachment, $file );
            require_once( ABSPATH . 'wp-admin/includes/image.php' );
            $attach_data = wp_generate_attachment_metadata( $attachment_id , $file );
            wp_update_attachment_metadata( $attachment_id , $attach_data );

It only shows the 'default' image file in media library when I submit the form.

Please advise what I am doing wrong. Thanks!

本文标签: uploadsMedia Library does not show the uploaded image