Php 生成在WordPress中存储图像的POST请求

Php 生成在WordPress中存储图像的POST请求,php,wordpress,image-upload,Php,Wordpress,Image Upload,我正在与WordPress合作,将图像保存到其媒体库中。图像以base64格式发送,因此我想知道我们如何在解码base64图像后对其发出POST数据请求,因为WordPress的wp\u handle\u upload函数不接受通过url发送的图像 这是我的密码 $image = $_REQUEST['image']; // requested image include('../wp-load.php'); // Load wordpress engine include_once('co

我正在与WordPress合作,将图像保存到其媒体库中。图像以base64格式发送,因此我想知道我们如何在解码base64图像后对其发出POST数据请求,因为WordPress的
wp\u handle\u 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图像写入uploads文件夹,然后将其作为附件插入

$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 );
$postId=$\u请求['postId'];
$image=$_请求['image'];
$directory=“/”.date(Y)。“/”.date(m)。“/”;
$wp_upload_dir=wp_upload_dir();
$data=base64_解码($image);
$filename=“IMG_u”.time()。.png”;
//$fileurl=$wp\u upload\u dir['url']./'。basename($filename);
$fileurl=“../wp content/uploads”。$directory.$filename;
$filetype=wp\u check\u文件类型(basename($fileurl),null);
文件内容($fileurl,$data);
$attachment=array(
'guid'=>$wp\u upload\u dir['url']./'.basename($fileurl),
'post_mime_type'=>$filetype['type'],
'post\u title'=>preg\u replace('/\.[^.]+$/','',basename($fileurl)),
'发布内容'=>'',
“post_状态”=>“继承”
);
//打印(附件);
//echo“
文件名:$fileurl”; $attach\u id=wp\u insert\u attachment($attachment,$fileurl,$postId); 需要一次('../wp admin/includes/image.php'); //为附件生成元数据,并更新数据库记录。 $attach\u data=wp\u生成\u附件\u元数据($attach\u id,$fileurl); wp_更新_附件_元数据($attach_id,$attach_data);

希望它能帮助某人:)

我用另一种方法做了,我将解码后的base64图像写入uploads文件夹,然后将其作为附件插入

$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 );
$postId=$\u请求['postId'];
$image=$_请求['image'];
$directory=“/”.date(Y)。“/”.date(m)。“/”;
$wp_upload_dir=wp_upload_dir();
$data=base64_解码($image);
$filename=“IMG_u”.time()。.png”;
//$fileurl=$wp\u upload\u dir['url']./'。basename($filename);
$fileurl=“../wp content/uploads”。$directory.$filename;
$filetype=wp\u check\u文件类型(basename($fileurl),null);
文件内容($fileurl,$data);
$attachment=array(
'guid'=>$wp\u upload\u dir['url']./'.basename($fileurl),
'post_mime_type'=>$filetype['type'],
'post\u title'=>preg\u replace('/\.[^.]+$/','',basename($fileurl)),
'发布内容'=>'',
“post_状态”=>“继承”
);
//打印(附件);
//echo“
文件名:$fileurl”; $attach\u id=wp\u insert\u attachment($attachment,$fileurl,$postId); 需要一次('../wp admin/includes/image.php'); //为附件生成元数据,并更新数据库记录。 $attach\u data=wp\u生成\u附件\u元数据($attach\u id,$fileurl); wp_更新_附件_元数据($attach_id,$attach_data);

希望它能对某人有所帮助:)

使用您的方法添加图像时是否尝试删除图像?在这种情况下,wp_delete_附件不适用于我。。(可能是因为图像不是通过wp_handle_upload插入的?)我遇到了一些服务器限制问题。我无法通过相对路径访问根文件夹。我不得不改用ABSPATH。因此,我编辑了几行代码,如`$filetype=wp\u check\u filetype(basename($fileurl),null);需要一次(ABSPATH./wp admin/includes/image.php')`使用您的方法添加图像时是否尝试删除图像?在这种情况下,wp_delete_附件不适用于我。。(可能是因为图像不是通过wp_handle_upload插入的?)我遇到了一些服务器限制问题。我无法通过相对路径访问根文件夹。我不得不改用ABSPATH。因此,我编辑了几行代码,如`$filetype=wp\u check\u filetype(basename($fileurl),null);需要一次(ABSPATH./wp admin/includes/image.php')`