media_handle_sideload()

最后更新于:2021-11-27 14:44:01

media_handle_sideload( string[]$file_array, int$post_id, string$desc=null, array$post_data=array())

Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().

参数

$file_array

(string[]) (Required) Array that represents a $_FILES upload array.

$post_id

(int) (Optional) The post ID the media is associated with.

$desc

(string) (Optional) Description of the side-loaded file.

Default value: null

$post_data

(array) (Optional) Post data to override.

Default value: array()

响应

(int|GC_Error) The ID of the attachment or a GC_Error on failure.

源文件

文件: gc-admin/includes/media.php

function media_handle_sideload( $file_array, $post_id = 0, $desc = null, $post_data = array() ) {
	$overrides = array( 'test_form' => false );

	if ( isset( $post_data['post_date'] ) && substr( $post_data['post_date'], 0, 4 ) > 0 ) {
		$time = $post_data['post_date'];
	} else {
		$post = get_post( $post_id );
		if ( $post && substr( $post->post_date, 0, 4 ) > 0 ) {
			$time = $post->post_date;
		} else {
			$time = current_time( 'mysql' );
		}
	}

	$file = gc_handle_sideload( $file_array, $overrides, $time );

	if ( isset( $file['error'] ) ) {
		return new GC_Error( 'upload_error', $file['error'] );
	}

	$url     = $file['url'];
	$type    = $file['type'];
	$file    = $file['file'];
	$title   = preg_replace( '/.[^.]+$/', '', gc_basename( $file ) );
	$content = '';

	// Use image exif/iptc data for title and caption defaults if possible.
	$image_meta = gc_read_image_metadata( $file );

	if ( $image_meta ) {
		if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {
			$title = $image_meta['title'];
		}

		if ( trim( $image_meta['caption'] ) ) {
			$content = $image_meta['caption'];
		}
	}

	if ( isset( $desc ) ) {
		$title = $desc;
	}

	// Construct the attachment array.
	$attachment = array_merge(
		array(
			'post_mime_type' => $type,
			'guid'           => $url,
			'post_parent'    => $post_id,
			'post_title'     => $title,
			'post_content'   => $content,
		),
		$post_data
	);

	// This should never be set as it would then overwrite an existing attachment.
	unset( $attachment['ID'] );

	// Save the attachment metadata.
	$attachment_id = gc_insert_attachment( $attachment, $file, $post_id, true );

	if ( ! is_gc_error( $attachment_id ) ) {
		gc_update_attachment_metadata( $attachment_id, gc_generate_attachment_metadata( $attachment_id, $file ) );
	}

	return $attachment_id;
}
<?php

/*
 * Build the $file_array with
 * $url = the url of the image
 * $temp = storing the image in gechiui
 */

$url = 'https://s.w.org/about/images/logos/gechiui-logo-stacked-rgb.png';

$tmp = download_url( $url );

$file_array = array(
    'name' => basename( $url ),
    'tmp_name' => $tmp
);

/**
 * Check for download errors
 * if there are error unlink the temp file name
 */
if ( is_gc_error( $tmp ) ) {
    @unlink( $file_array[ 'tmp_name' ] );
    return $tmp;
}

/**
 * now we can actually use media_handle_sideload
 * we pass it the file array of the file to handle
 * and the post id of the post to attach it to
 * $post_id can be set to '0' to not attach it to any particular post
 */
$post_id = '0';

$id = media_handle_sideload( $file_array, $post_id );

/**
 * We don't want to pass something to $id
 * if there were upload errors.
 * So this checks for errors
 */
if ( is_gc_error( $id ) ) {
    @unlink( $file_array['tmp_name'] );
    return $id;
}

/**
 * No we can get the url of the sideloaded file
 * $value now contains the file url in GeChiUI
 * $id is the attachment id
 */
$value = gc_get_attachment_url( $id );

// Now you can do something with $value (or $id)