^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 5年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云1核2g仅需102元/年,今日10点开抢

wordpress函数download_url()用法示例

download_url( string $url,  int $timeout = 300 )

Downloads a URL to a local temporary file using the WordPress HTTP Class.


描述

Please note, That the calling function must unlink() the file.


参数

$url

(string)
(Required)
the URL of the file to download

$timeout

(int)
(Optional)
The timeout for the request to download the file default 300 seconds

Default value: 300


返回值

(mixed) WP_Error on failure, string Filename on success.


源代码

File: wp-admin/includes/file.php

function download_url( $url, $timeout = 300 ) {
	//WARNING: The file is not automatically deleted, The script must unlink() the file.
	if ( ! $url )
		return new WP_Error('http_no_url', __('Invalid URL Provided.'));

	$url_filename = basename( parse_url( $url, PHP_URL_PATH ) );

	$tmpfname = wp_tempnam( $url_filename );
	if ( ! $tmpfname )
		return new WP_Error('http_no_file', __('Could not create Temporary file.'));

	$response = wp_safe_remote_get( $url, array( 'timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname ) );

	if ( is_wp_error( $response ) ) {
		unlink( $tmpfname );
		return $response;
	}

	if ( 200 != wp_remote_retrieve_response_code( $response ) ){
		unlink( $tmpfname );
		return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ) );
	}

	$content_md5 = wp_remote_retrieve_header( $response, 'content-md5' );
	if ( $content_md5 ) {
		$md5_check = verify_file_md5( $tmpfname, $content_md5 );
		if ( is_wp_error( $md5_check ) ) {
			unlink( $tmpfname );
			return $md5_check;
		}
	}

	return $tmpfname;
}

更新日志

Version 描述
2.5.0 Introduced.

相关函数

Uses

  • wp-admin/includes/file.php:
    wp_tempnam()
  • wp-admin/includes/file.php:
    verify_file_md5()
  • wp-includes/l10n.php:
    __()
  • wp-includes/http.php:
    wp_safe_remote_get()
  • wp-includes/http.php:
    wp_remote_retrieve_response_code()
  • wp-includes/http.php:
    wp_remote_retrieve_response_message()
  • wp-includes/http.php:
    wp_remote_retrieve_header()
  • wp-includes/load.php:
    is_wp_error()
  • wp-includes/class-wp-error.php:
    WP_Error::__construct()
  • Show 4 more uses
    Hide more uses


Used By

  • wp-admin/includes/class-wp-upgrader.php:
    WP_Upgrader::download_package()
  • wp-admin/includes/media.php:
    media_sideload_image()

User Contributed Notes

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈