图片显示在wordpress本地上传文件夹中,但未在wordpress媒体库(wp仪表板)中更新

我试图在子主题中使用wp自定义模板上传图像,但是当我上传任何图像时。它显示在“ E:\ Xamp \ htdocs \ website \ wp-content \ uploads \ 2019 \ 10”中,但未上传到wp仪表盘媒体库中。

此任务我不允许使用任何插件。

$post_id = wp_insert_post($my_post);

if(isset($_FILES['file']['name'])){
    if(! function_exists('wp_handle_upload')){
        require_once(ABSPATH.'wp-admin/includes/file.php');
    }
    $uploadfile = $_FILES['file'];
    print_r($uploadfile);
    $upload_overrides = array('test_form' => false );

    $moveupload = wp_handle_upload($uploadfile,$upload_overrides);
    if($moveupload && ! isset($moveupload['error'])){
        echo "</Pre";
        wp_update_attachment_metadata( $post_id,$moveupload);
        print_r($moveupload);
        echo "Post/>";
    }else{
        echo $moveupload['error'];
    }
}
kx287973135 回答:图片显示在wordpress本地上传文件夹中,但未在wordpress媒体库(wp仪表板)中更新

将文件上传到wp-content / ups中不会显示在媒体库中,那些媒体ID需要在数据库中才能显示在媒体库中。

如果您的上传文件夹中已经有文件,并且想要将其添加到数据库中

但这不是正确的解决方案,而是解决该上载文件夹的权限问题。

谢谢

,

我通过使用自定义php代码解决了该问题。

$upload = wp_upload_bits($_FILES["file"]["name"],null,file_get_contents($_FILES["file"]["tmp_name"]));
$filename = $upload['file'];
$wp_filetype = wp_check_filetype($filename,null );
// print_r($filename);
$attachment = array(
    'post_mime_type' => $wp_filetype['type'],'post_title' => sanitize_file_name($filename),'post_content' => '','post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment,$filename,$post_id );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id,$filename );
wp_update_attachment_metadata( $attach_id,$attach_data );
    set_post_thumbnail( $post_id,$attach_id );
,
Can try out another piece of code here:
--------------------------
$upload_overrides = array( "test_form" => false );

$uploaded_file = wp_handle_upload ($file,$upload_overrides);

if( isset( $uploaded_file ["file"] )) {
    $file_name_and_location = $uploaded_file ["file"];
    $file_title_for_media_library = $title;

    $attachment = array(
        "post_mime_type" => $uploaded_file_type,"post_title" => addslashes( $file_title_for_media_library ),"post_content" => "","post_status" => "inherit"
    );

    if( ! is_null( $post )) {
        if ( ! is_numeric( $post )) {
            $post = $post->ID;
        } // if ()

        $attachment ['post_parent'] = $post;
    } // if ()

    $id = wp_insert_attachment( $attachment,$file_name_and_location );

    require_once( ABSPATH."wp-admin/includes/image.php" );

    $attach_data = wp_generate_attachment_metadata( $id,$file_name_and_location );
    wp_update_attachment_metadata( $id,$attach_data );
} // if ()
本文链接:https://www.f2er.com/3162220.html

大家都在问