生成 POST 请求以在 WordPress 中存储图像
Generate POST request for storing images in WordPress
我正在使用 WordPress 将图像保存到它的媒体库中。图像以 base64 格式发送,所以我想知道我们如何在解码后对该 base64 图像发出 POST 数据请求,因为 WordPress 的 wp_handle_upload
函数不通过 url 接受图像。
这是我的代码
$image = $_REQUEST['image']; // requested image
include('../wp-load.php'); // Load wordpress engine
include_once('config.php');
$data = base64_decode($image); // decode image
$filename = "IMG_".time().".png"; // filename
//$fileurl = "../wp-content/uploads".$directory.$filename;
//file_put_contents($fileurl, $data);
// wordpress wp_handle_upload not accept image via url
if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
$uploadedfile = $data;
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile ) {
// echo "file loaded";
}
if ( $movefile ) {
$wp_filetype = $movefile['type'];
$filename = $movefile['file'];
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $wp_filetype,
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename);
}
我用另一种方式做了,我将解码的 base64 图像写入上传文件夹,然后将其作为附件插入
$postId = $_REQUEST['postId'];
$image = $_REQUEST['image'];
$directory = "/".date(Y)."/".date(m)."/";
$wp_upload_dir = wp_upload_dir();
$data = base64_decode($image);
$filename = "IMG_".time().".png";
//$fileurl = $wp_upload_dir['url'] . '/' . basename( $filename );
$fileurl = "../wp-content/uploads".$directory.$filename;
$filetype = wp_check_filetype( basename( $fileurl), null );
file_put_contents($fileurl, $data);
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $fileurl ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($fileurl)),
'post_content' => '',
'post_status' => 'inherit'
);
// print_r($attachment);
//echo "<br>file name : $fileurl";
$attach_id = wp_insert_attachment( $attachment, $fileurl ,$postId);
require_once('../wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $fileurl );
wp_update_attachment_metadata( $attach_id, $attach_data );
希望它能对某人有所帮助:)
我正在使用 WordPress 将图像保存到它的媒体库中。图像以 base64 格式发送,所以我想知道我们如何在解码后对该 base64 图像发出 POST 数据请求,因为 WordPress 的 wp_handle_upload
函数不通过 url 接受图像。
这是我的代码
$image = $_REQUEST['image']; // requested image
include('../wp-load.php'); // Load wordpress engine
include_once('config.php');
$data = base64_decode($image); // decode image
$filename = "IMG_".time().".png"; // filename
//$fileurl = "../wp-content/uploads".$directory.$filename;
//file_put_contents($fileurl, $data);
// wordpress wp_handle_upload not accept image via url
if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
$uploadedfile = $data;
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile ) {
// echo "file loaded";
}
if ( $movefile ) {
$wp_filetype = $movefile['type'];
$filename = $movefile['file'];
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $wp_filetype,
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename);
}
我用另一种方式做了,我将解码的 base64 图像写入上传文件夹,然后将其作为附件插入
$postId = $_REQUEST['postId'];
$image = $_REQUEST['image'];
$directory = "/".date(Y)."/".date(m)."/";
$wp_upload_dir = wp_upload_dir();
$data = base64_decode($image);
$filename = "IMG_".time().".png";
//$fileurl = $wp_upload_dir['url'] . '/' . basename( $filename );
$fileurl = "../wp-content/uploads".$directory.$filename;
$filetype = wp_check_filetype( basename( $fileurl), null );
file_put_contents($fileurl, $data);
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $fileurl ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($fileurl)),
'post_content' => '',
'post_status' => 'inherit'
);
// print_r($attachment);
//echo "<br>file name : $fileurl";
$attach_id = wp_insert_attachment( $attachment, $fileurl ,$postId);
require_once('../wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $fileurl );
wp_update_attachment_metadata( $attach_id, $attach_data );
希望它能对某人有所帮助:)