diff --git a/wp-admin/includes/file.php b/wp-admin/includes/file.php index cb80ce2b55..c4dd62923a 100644 --- a/wp-admin/includes/file.php +++ b/wp-admin/includes/file.php @@ -423,20 +423,26 @@ function wp_handle_sideload( &$file, $overrides = false ) { */ function download_url( $url ) { //WARNING: The file is not automatically deleted, The script must unlink() the file. - if( ! $url ) + if ( ! $url ) return new WP_Error('http_no_url', __('Invalid URL Provided')); $tmpfname = wp_tempnam($url); - if( ! $tmpfname ) + if ( ! $tmpfname ) return new WP_Error('http_no_file', __('Could not create Temporary file')); $handle = @fopen($tmpfname, 'w'); - if( ! $handle ) + if ( ! $handle ) return new WP_Error('http_no_file', __('Could not create Temporary file')); $response = wp_remote_get($url); - if( $response['response']['code'] != '200' ){ + if ( is_wp_error($response) ) { + fclose($handle); + unlink($tmpfname); + return $response; + } + + if ( $response['response']['code'] != '200' ){ fclose($handle); unlink($tmpfname); return new WP_Error('http_404', trim($response['response']['message'])); diff --git a/wp-includes/http.php b/wp-includes/http.php index f04f74e9f8..911ba50b02 100644 --- a/wp-includes/http.php +++ b/wp-includes/http.php @@ -977,7 +977,7 @@ class WP_Http_Curl { $theResponse = curl_exec( $handle ); - if ( $theResponse ) { + if ( !empty($theResponse) ) { $headerLength = curl_getinfo($handle, CURLINFO_HEADER_SIZE); $theHeaders = trim( substr($theResponse, 0, $headerLength) ); $theBody = substr( $theResponse, $headerLength ); @@ -987,8 +987,11 @@ class WP_Http_Curl { } $theHeaders = WP_Http::processHeaders($theHeaders); } else { + if ( $curl_error = curl_error($handle) ) + return new WP_Error('http_request_failed', $curl_error); if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array(301, 302) ) ) return new WP_Error('http_request_failed', __('Too many redirects.')); + $theHeaders = array( 'headers' => array() ); $theBody = ''; }