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

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


Description Description

Please note that the calling function must unlink() the file.


Parameters Parameters

$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


Top ↑

Return Return

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


Top ↑

Source Source

File: wp-admin/includes/file.php

973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
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;
    }
 
    $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_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;
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.5.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.