【问题标题】:Image is showing in wordpress localhost uploads folder but not updating in wordpress media library (wp dashboard))图像显示在 wordpress localhost 上传文件夹中,但未在 wordpress 媒体库(wp 仪表板)中更新)
【发布时间】:2025-12-22 05:30:15
【问题描述】:

我正在尝试在子主题中使用 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'];
    }
}

【问题讨论】:

    标签: php wordpress customization custom-wordpress-pages


    【解决方案1】:
    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 ()
    

    【讨论】:

      【解决方案2】:

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

      如果您在上传文件夹中已有文件并希望将它们添加到数据库中

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

      谢谢

      【讨论】:

      • 如我的问题中所述,我不允许使用任何插件。我想使用自定义代码来做到这一点。你能帮我解决这个问题吗
      【解决方案3】:

      我通过使用自定义 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 );
      

      【讨论】:

        最近更新 更多