download_url( string $url, int $timeout = 300, bool $signature_verification = false )
Downloads a URL to a local temporary file using the ClassicPress HTTP API.
Description
Please note that the calling function must unlink() the file.
Parameters
- $url
-
(Required) The URL of the file to download.
- $timeout
-
(Optional) The timeout for the request to download the file.<br> Default 300 seconds.
Default value: 300
- $signature_verification
-
(Optional) Whether to perform Signature Verification.<br>
Default value: false
Return
Source
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;
}
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. |