我正在尝试使用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 );
但是当我尝试运行它时,我有这个错误代码:
警告:require(ABSPATHWPINC / option.php):无法打开流:/home/sales/domains/lilopel.com/public_html/wouter/wordpress/wp-includes/functions.php中没有此类文件或目录第8行
警告:require(ABSPATHWPINC / option.php):无法打开流:/home/sales/domains/lilopel.com/public_html/wouter/wordpress/wp-includes/functions.php中没有此类文件或目录第8行
致命错误:require():在/home/sales/domains/lilopel.com/public_html/中打开所需的'ABSPATHWPINC / option.php'(include_path ='。:/ usr / local / lib / php')失败第8行的wouter / wordpress / wp-includes / functions.php
有没有人知道如何修复此错误?
答案 0 :(得分:0)
上传表单可能如下所示:
<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.
}