函数文档

download_url()

💡 云策文档标注

概述

download_url() 函数使用 WordPress HTTP API 将 URL 下载到本地临时文件。调用者需负责文件删除或移动,函数返回文件名或 WP_Error。

关键要点

  • 函数参数:$url(必需,下载文件的 URL)、$timeout(可选,默认 300 秒)、$signature_verification(可选,默认 false,是否执行签名验证)。
  • 返回类型:成功时返回临时文件名(字符串),失败时返回 WP_Error。
  • 重要警告:文件不会自动删除,调用函数必须删除或移动文件,否则可能导致临时文件累积。
  • 功能特性:支持从 Content-Disposition 头提取文件名、根据 MIME 类型重命名文件、MD5 验证、签名验证(可配置主机列表和软失败选项)。
  • 相关 Hook:包括 download_url_error_max_body_size、wp_signature_hosts、wp_signature_softfail、wp_signature_url 等过滤器。
  • 使用场景:常用于 WP_Upgrader::download_package() 和 media_sideload_image() 等核心功能。

代码示例

function download_url( $url, $timeout = 300, $signature_verification = false ) {
    // 函数实现代码(详见正文)
}

注意事项

  • 在前端或 cron 任务中使用时,需包含 wp-admin/includes/file.php 文件。
  • 避免使用 unlink() 删除文件,推荐使用 wp_delete_file() 以符合 WordPress 编码标准。
  • 不推荐通过直接调用 wp-load.php 来使用此函数,应使用 WordPress 标准加载方式。

📄 原文内容

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

Description

Please note that the calling function must delete or move the file.

Parameters

$urlstringrequired
The URL of the file to download.
$timeoutintoptional
The timeout for the request to download the file.
Default 300 seconds.

Default:300

$signature_verificationbooloptional
Whether to perform Signature Verification.

Default:false

Return

string|WP_Error Filename on success, WP_Error on failure.

Source

function download_url( $url, $timeout = 300, $signature_verification = false ) {
	// WARNING: The file is not automatically deleted, the script must delete or move the file.
	if ( ! $url ) {
		return new WP_Error( 'http_no_url', __( 'No URL Provided.' ) );
	}

	$url_path     = parse_url( $url, PHP_URL_PATH );
	$url_filename = '';
	if ( is_string( $url_path ) && '' !== $url_path ) {
		$url_filename = basename( $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;
	}

	$response_code = wp_remote_retrieve_response_code( $response );

	if ( 200 !== $response_code ) {
		$data = array(
			'code' => $response_code,
		);

		// Retrieve a sample of the response body for debugging purposes.
		$tmpf = fopen( $tmpfname, 'rb' );

		if ( $tmpf ) {
			/**
			 * Filters the maximum error response body size in `download_url()`.
			 *
			 * @since 5.1.0
			 *
			 * @see download_url()
			 *
			 * @param int $size The maximum error response body size. Default 1 KB.
			 */
			$response_size = apply_filters( 'download_url_error_max_body_size', KB_IN_BYTES );

			$data['body'] = fread( $tmpf, $response_size );
			fclose( $tmpf );
		}

		unlink( $tmpfname );

		return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ), $data );
	}

	$content_disposition = wp_remote_retrieve_header( $response, 'Content-Disposition' );

	if ( $content_disposition ) {
		$content_disposition = strtolower( $content_disposition );

		if ( str_starts_with( $content_disposition, 'attachment; filename=' ) ) {
			$tmpfname_disposition = sanitize_file_name( substr( $content_disposition, 21 ) );
		} else {
			$tmpfname_disposition = '';
		}

		// Potential file name must be valid string.
		if ( $tmpfname_disposition && is_string( $tmpfname_disposition )
			&& ( 0 === validate_file( $tmpfname_disposition ) )
		) {
			$tmpfname_disposition = dirname( $tmpfname ) . '/' . $tmpfname_disposition;

			if ( rename( $tmpfname, $tmpfname_disposition ) ) {
				$tmpfname = $tmpfname_disposition;
			}

			if ( ( $tmpfname !== $tmpfname_disposition ) && file_exists( $tmpfname_disposition ) ) {
				unlink( $tmpfname_disposition );
			}
		}
	}

	$mime_type = wp_remote_retrieve_header( $response, 'content-type' );
	if ( $mime_type && 'tmp' === pathinfo( $tmpfname, PATHINFO_EXTENSION ) ) {
		$valid_mime_types = array_flip( get_allowed_mime_types() );
		if ( ! empty( $valid_mime_types[ $mime_type ] ) ) {
			$extensions     = explode( '|', $valid_mime_types[ $mime_type ] );
			$new_image_name = substr( $tmpfname, 0, -4 ) . ".{$extensions[0]}";
			if ( 0 === validate_file( $new_image_name ) ) {
				if ( rename( $tmpfname, $new_image_name ) ) {
					$tmpfname = $new_image_name;
				}

				if ( ( $tmpfname !== $new_image_name ) && file_exists( $new_image_name ) ) {
					unlink( $new_image_name );
				}
			}
		}
	}

	$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;
		}
	}

	// If the caller expects signature verification to occur, check to see if this URL supports it.
	if ( $signature_verification ) {
		/**
		 * Filters the list of hosts which should have Signature Verification attempted on.
		 *
		 * @since 5.2.0
		 *
		 * @param string[] $hostnames List of hostnames.
		 */
		$signed_hostnames = apply_filters( 'wp_signature_hosts', array( 'wordpress.org', 'downloads.wordpress.org', 's.w.org' ) );

		$signature_verification = in_array( parse_url( $url, PHP_URL_HOST ), $signed_hostnames, true );
	}

	// Perform signature validation if supported.
	if ( $signature_verification ) {
		$signature = wp_remote_retrieve_header( $response, 'X-Content-Signature' );

		if ( ! $signature ) {
			/*
			 * Retrieve signatures from a file if the header wasn't included.
			 * WordPress.org stores signatures at $package_url.sig.
			 */

			$signature_url = false;

			if ( is_string( $url_path ) && ( str_ends_with( $url_path, '.zip' ) || str_ends_with( $url_path, '.tar.gz' ) ) ) {
				$signature_url = str_replace( $url_path, $url_path . '.sig', $url );
			}

			/**
			 * Filters the URL where the signature for a file is located.
			 *
			 * @since 5.2.0
			 *
			 * @param false|string $signature_url The URL where signatures can be found for a file, or false if none are known.
			 * @param string $url                 The URL being verified.
			 */
			$signature_url = apply_filters( 'wp_signature_url', $signature_url, $url );

			if ( $signature_url ) {
				$signature_request = wp_safe_remote_get(
					$signature_url,
					array(
						'limit_response_size' => 10 * KB_IN_BYTES, // 10KB should be large enough for quite a few signatures.
					)
				);

				if ( ! is_wp_error( $signature_request ) && 200 === wp_remote_retrieve_response_code( $signature_request ) ) {
					$signature = explode( "n", wp_remote_retrieve_body( $signature_request ) );
				}
			}
		}

		// Perform the checks.
		$signature_verification = verify_file_signature( $tmpfname, $signature, $url_filename );
	}

	if ( is_wp_error( $signature_verification ) ) {
		if (
			/**
			 * Filters whether Signature Verification failures should be allowed to soft fail.
			 *
			 * WARNING: This may be removed from a future release.
			 *
			 * @since 5.2.0
			 *
			 * @param bool   $signature_softfail If a softfail is allowed.
			 * @param string $url                The url being accessed.
			 */
			apply_filters( 'wp_signature_softfail', true, $url )
		) {
			$signature_verification->add_data( $tmpfname, 'softfail-filename' );
		} else {
			// Hard-fail.
			unlink( $tmpfname );
		}

		return $signature_verification;
	}

	return $tmpfname;
}

Hooks

apply_filters( ‘download_url_error_max_body_size’, int $size )

Filters the maximum error response body size in download_url().

apply_filters( ‘wp_signature_hosts’, string[] $hostnames )

Filters the list of hosts which should have Signature Verification attempted on.

apply_filters( ‘wp_signature_softfail’, bool $signature_softfail, string $url )

Filters whether Signature Verification failures should be allowed to soft fail.

apply_filters( ‘wp_signature_url’, false|string $signature_url, string $url )

Filters the URL where the signature for a file is located.

Changelog

Version Description
5.9.0 Support for Content-Disposition filename was added.
5.2.0 Signature Verification with SoftFail was added.
2.5.0 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    To be able to use this function in the front-end nor cron you must include wp-admin/includes/file.php file.

    </pre>
    				</div><!-- .comment-content -->
    
    					<section id='feedback-3048' class='wporg-has-embedded-code feedback hide-if-js' data-comment-count='0'>
    </section><!-- .feedback -->
    <footer class='feedback-links wporg-dot-link-list' >
    <a role="button" class="feedback-login" href="https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fdownload_url%2F%3Freplytocom%3D3048%23feedback-editor-3048" rel="nofollow">Log in to add feedback</a></footer>
    </article><!-- .comment-body -->
    </li>
    			<li id="comment-6738" data-comment-id="6738" class="comment odd alt thread-even depth-1">
    			<article id="div-comment-6738" class="comment-body">
    
    							<a href="#comment-content-6738" class="screen-reader-text">Skip to note 5 content</a>
    				<header class="comment-meta">
    					<div class="comment-author vcard">
    						<span class="comment-author-attribution">
    						Anonymous User						</span>
    						<a class="comment-date" href="https://developer.wordpress.org/reference/functions/download_url/#comment-6738">
    							<time datetime="2023-10-28T05:14:55+00:00">
    							2 years ago							</time>
    						</a>
    
    																													</div>
    					<div class="user-note-voting" data-nonce="52a81f28af" data-can-vote="false"><a class="user-note-voting-up" title="You must log in to vote on the helpfulness of this note" data-id="6738" data-vote="up" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fdownload_url%2F%23comment-6738"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a><span class="user-note-voting-count " title=""><span class="screen-reader-text">Vote results for this note: </span>0</span><a class="user-note-voting-down" title="You must log in to vote on the helpfulness of this note" data-id="6738" data-vote="down" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fdownload_url%2F%23comment-6738"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a></div>				</header>
    				<!-- .comment-metadata -->
    			
    				<div class="wporg-has-embedded-code comment-content" id="comment-content-6738">
    				<p><code>unlink()</code> (and worse, its <code>@</code> silenced version) is discouraged in WPCS.<br />
    Use <a href="https://developer.wordpress.org/reference/functions/wp_delete_file/" rel="ugc"><code>wp_delete_file()</code></a> instead.</p>
    				</div><!-- .comment-content -->
    
    					<section id='feedback-6738' class='wporg-has-embedded-code feedback hide-if-js' data-comment-count='0'>
    </section><!-- .feedback -->
    <footer class='feedback-links wporg-dot-link-list' >
    <a role="button" class="feedback-login" href="https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fdownload_url%2F%3Freplytocom%3D6738%23feedback-editor-6738" rel="nofollow">Log in to add feedback</a></footer>
    </article><!-- .comment-body -->
    </li>
    			<li id="comment-3422" data-comment-id="3422" class="comment byuser comment-author-marcio-zebedeu even thread-odd thread-alt depth-1 bad-note">
    			<article id="div-comment-3422" class="comment-body">
    
    							<a href="#comment-content-3422" class="screen-reader-text">Skip to note 6 content</a>
    				<header class="comment-meta">
    					<div class="comment-author vcard">
    						<span class="comment-author-attribution">
    						<a href="https://profiles.wordpress.org/marcio-zebedeu/" rel="external nofollow" class="url">Marcio Zebedeu</a>						</span>
    						<a class="comment-date" href="https://developer.wordpress.org/reference/functions/download_url/#comment-3422">
    							<time datetime="2019-10-18T00:16:57+00:00">
    							7 years ago							</time>
    						</a>
    
    																													</div>
    					<div class="user-note-voting" data-nonce="2cfd914a0e" data-can-vote="false"><a class="user-note-voting-up" title="You must log in to vote on the helpfulness of this note" data-id="3422" data-vote="up" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fdownload_url%2F%23comment-3422"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a><span class="user-note-voting-count " title="0% like this"><span class="screen-reader-text">Vote results for this note: </span>-6</span><a class="user-note-voting-down" title="You must log in to vote on the helpfulness of this note" data-id="3422" data-vote="down" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fdownload_url%2F%23comment-3422"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a></div>				</header>
    				<!-- .comment-metadata -->
    			
    				<div class="wporg-has-embedded-code comment-content" id="comment-content-3422">
    				<p>WordPress files can be called easily by accessing the wp-load file.php which is located at the root of wordpress installation.</p>
    <pre class="wp-block-code"><code lang="php" class="language-php ">    require_once(BASE_PATH . 'wp-load.php');
        /*
            link to file to be downloaded
        */
    	    public function download( $url = "<a href="http://www.example.com/example/downloads/information/php.pdf&quot" rel="nofollow ugc">http://www.example.com/example/downloads/information/php.pdf"</a>;){
            download_url( $url );
    }