Quantcast
Channel: BuddyPress实验室
Viewing all articles
Browse latest Browse all 34

Wordpress通过远程地址上传图片

$
0
0

Wordpress的图片上传,默认的都是从本地上传,但如果想从远程抓取,或是通过远程地址获取的话,就要用到wp_upload_bits。

wp_upload_bits介绍:

用法

<?php wp_upload_bits( $name, $deprecated, $bits, $time ) ?>

参数

$name

(string) (required)

Default: None

$deprecated

(null) (required) Not used. Set to null.

Default: None

$bits

(mixed) (required) File content

Default: None

$time

(string) (optional) Time formatted in ‘yyyy/mm’.

Default: null

返回值

返回array值

file

文件上传路径 (e.g. /var/www/wordpress/wp-content/uploads/2010/03/example.pdf)

url

文件上传完整路径 (e.g. http://example.com/wp-content/uploads/2010/03/example.pdf)

error

如果产生错误,就返回,否则返回false

实现上传代码片段

$get = wp_remote_get( $_POST[‘url’] );

//$_POST[‘url’]获取到的附件地址

$file_content = wp_remote_retrieve_body($get);

$uploaded_file = wp_upload_bits($file_name,null,$file_content);

$wp_filetype = wp_check_filetype( basename( $uploaded_file[‘file’] ), null );

$attachment = array(

‘post_mime_type’ => $wp_filetype[‘type’],

‘post_title’ => basename($file_name),

‘post_author’ => $logged_in_user,

‘post_content’ => ”,

‘post_status’ => ‘inherit’,

‘post_type’ => ‘attachment’,

‘post_parent’ => $_POST[‘post_id’],

‘guid’ => $uploaded_file[‘url’],

);

$attachment_id = wp_insert_post( $attachment );


Viewing all articles
Browse latest Browse all 34

Trending Articles