返回介绍

wp_handle_sideload()

发布于 2017-09-11 12:10:19 字数 4380 浏览 1172 评论 0 收藏 0

wp_handle_sideload( array $file,  array|bool $overrides = false,  string $time = null )

Wrapper for _wp_handle_upload().


description

Passes the ‘wp_handle_sideload’ action.


参数

$file

(array) (Required) An array similar to that of a PHP $_FILES POST array

$overrides

(array|bool) (Optional) An associative array of names=>values to override default variables.

Default value: false

$time

(string) (Optional) Time formatted in 'yyyy/mm'.

Default value: null


返回值

(array) On success, returns an associative array of file attributes. On failure, returns $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ).


源代码

File: wp-admin/includes/file.php

function wp_handle_sideload( &$file, $overrides = false, $time = null ) {
	/*
	 *  $_POST['action'] must be set and its value must equal $overrides['action']
	 *  or this:
	 */
	$action = 'wp_handle_sideload';
	if ( isset( $overrides['action'] ) ) {
		$action = $overrides['action'];
	}
	return _wp_handle_upload( $file, $overrides, $time, $action );
}

更新日志

Versiondescription
2.6.0Introduced.

相关函数

Uses

  • wp-admin/includes/file.php: _wp_handle_upload()

Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php: WP_REST_Attachments_Controller::upload_from_data()
  • wp-admin/includes/media.php: media_handle_sideload()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by mattmckenny

    The returned array of file attributes includes values for these keys

    url
    type
    file

  2. Example

    This example uses download_url() to download the logo from wordpress.org and then moves it into the uploads directory.

    
    // Gives us access to the download_url() and wp_handle_sideload() functions.
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    
    // URL to the WordPress logo.
    $url = 'https://s.w.org/style/images/wp-header-logo.png';
    $timeout_seconds = 5;
    
    // Download file to temp dir.
    $temp_file = download_url( $url, $timeout_seconds );
    
    if ( ! is_wp_error( $temp_file ) ) {
    
    	// Array based on $_FILE as seen in PHP file uploads.
    	$file = array(
    		'name'     => basename($url), // ex: wp-header-logo.png
    		'type'     => 'image/png',
    		'tmp_name' => $temp_file,
    		'error'    => 0,
    		'size'     => filesize( $temp_file ),
    	);
    
    	$overrides = array(
    		/*
    		 * Tells WordPress to not look for the POST form fields that would
    		 * normally be present, default is true, we downloaded the file from
    		 * a remote server, so there will be no form fields.
    		 */
    		'test_form' => false,
    
    		// Setting this to false lets WordPress allow empty files, not recommended.
    		'test_size' => true,
    
    		// A properly uploaded file will pass this test. There should be no reason to override this one.
    		'test_upload' => true, 
    	);
    
    	// Move the temporary file into the uploads directory.
    	$results = wp_handle_sideload( $file, $overrides );
    
    	if ( ! empty( $results['error'] ) ) {
    		// Insert any error handling here.
    	} else {
    		$filename  = $results['file']; // Full path to the file.
    		$local_url = $results['url'];  // URL to the file in the uploads dir.
    		$type      = $results['type']; // MIME type of the file.
    
    		// Perform any actions here based in the above results.
    	}
    }
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文