Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图用php代码将特色图片上传到wordpress数据库_Php_Mysql_Wordpress_.htaccess - Fatal编程技术网

试图用php代码将特色图片上传到wordpress数据库

试图用php代码将特色图片上传到wordpress数据库,php,mysql,wordpress,.htaccess,Php,Mysql,Wordpress,.htaccess,我正试图用PHPcode将外部图像上传到我的Wordpress数据库 这是我尝试使用的代码: <?php include ("../wordpress/wp-includes/post.php"); include ("../wordpress/wp-includes/functions.php"); include ("../wordpress/wp-admin/includes/image.php"); // $filename should be the path to a

我正试图用PHPcode将外部图像上传到我的Wordpress数据库

这是我尝试使用的代码:

<?php

include ("../wordpress/wp-includes/post.php");
include ("../wordpress/wp-includes/functions.php");
include ("../wordpress/wp-admin/includes/image.php");



// $filename should be the path to a file in the upload directory.
$filename = '../wordpress/wp-content/uploads/2016/02/220px-Smiley.svg.png';

// The ID of the post this attachment is for.
$parent_post_id = 22;

// 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 . '../wordpress/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 );

上传表单可能如下所示:

<form id="featured_upload" method="post" action="#" enctype="multipart/form-data">
    <input type="file" name="my_image_upload" id="my_image_upload"  multiple="false" />
    <input type="hidden" name="post_id" id="post_id" value="55" />
    <?php wp_nonce_field( 'my_image_upload', 'my_image_upload_nonce' ); ?>
    <input id="submit_my_image_upload" name="submit_my_image_upload" type="submit" value="Upload" />
</form>

保存附件的代码:

<?php

// Check that the nonce is valid, and the user can edit this post.
if ( 
    isset( $_POST['my_image_upload_nonce'], $_POST['post_id'] ) 
    && wp_verify_nonce( $_POST['my_image_upload_nonce'], 'my_image_upload' )
    && current_user_can( 'edit_post', $_POST['post_id'] )
) {
    // The nonce was valid and the user has the capabilities, it is safe to continue.

    // These files need to be included as dependencies when on the front end.
    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    require_once( ABSPATH . 'wp-admin/includes/media.php' );

    // Let WordPress handle the upload.
    // Remember, 'my_image_upload' is the name of our file input in our form above.
    $attachment_id = media_handle_upload( 'my_image_upload', $_POST['post_id'] );

    if ( is_wp_error( $attachment_id ) ) {
        // There was an error uploading the image.
    } else {
        // The image was uploaded successfully!
    }

} else {

    // The security check failed, maybe show the user an error.
}

你检查过了吗?请看下面的示例//这些文件在前端时需要作为依赖项包含。require_once(ABSPATH.'wp admin/includes/image.php');require_once(ABSPATH.'wp admin/includes/file.php');require_once(ABSPATH.'wp admin/includes/media.php');它返回:安全检查失败,可能向用户显示错误。我做错什么了吗?