Make WordPress Core


Ignore:
Timestamp:
03/25/2011 02:42:20 AM (14 years ago)
Author:
dd32
Message:

First run of introducing Stream-To-File for the WP_HTTP API. Reduces memory consumption during file downloads. Implemented in download_url() for upgraders. Props sivel. See #16236

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/file.php

    r17525 r17555  
    154154
    155155/**
    156  * Determines a writable directory for temporary files.
    157  * Function's preference is to WP_CONTENT_DIR followed by the return value of <code>sys_get_temp_dir()</code>, before finally defaulting to /tmp/
    158  *
    159  * In the event that this function does not find a writable location, It may be overridden by the <code>WP_TEMP_DIR</code> constant in your <code>wp-config.php</code> file.
    160  *
    161  * @since 2.5.0
    162  *
    163  * @return string Writable temporary directory
    164  */
    165 function get_temp_dir() {
    166     static $temp;
    167     if ( defined('WP_TEMP_DIR') )
    168         return trailingslashit(WP_TEMP_DIR);
    169 
    170     if ( $temp )
    171         return trailingslashit($temp);
    172 
    173     $temp = WP_CONTENT_DIR . '/';
    174     if ( is_dir($temp) && @is_writable($temp) )
    175         return $temp;
    176 
    177     if  ( function_exists('sys_get_temp_dir') ) {
    178         $temp = sys_get_temp_dir();
    179         if ( @is_writable($temp) )
    180             return trailingslashit($temp);
    181     }
    182 
    183     $temp = ini_get('upload_tmp_dir');
    184     if ( is_dir($temp) && @is_writable($temp) )
    185         return trailingslashit($temp);
    186 
    187     $temp = '/tmp/';
    188     return $temp;
    189 }
    190 
    191 /**
    192156 * Returns a filename of a Temporary unique file.
    193157 * Please note that the calling function must unlink() this itself.
     
    520484        return new WP_Error('http_no_file', __('Could not create Temporary file.'));
    521485
    522     $handle = @fopen($tmpfname, 'wb');
    523     if ( ! $handle )
    524         return new WP_Error('http_no_file', __('Could not create Temporary file.'));
    525 
    526     $response = wp_remote_get($url, array('timeout' => $timeout));
    527 
    528     if ( is_wp_error($response) ) {
    529         fclose($handle);
    530         unlink($tmpfname);
     486    $response = wp_remote_get( $url, array( 'timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname ) );
     487
     488    if ( is_wp_error( $response ) ) {
     489        unlink( $tmpfname );
    531490        return $response;
    532491    }
    533492
    534493    if ( $response['response']['code'] != '200' ){
    535         fclose($handle);
    536         unlink($tmpfname);
    537         return new WP_Error('http_404', trim($response['response']['message']));
    538     }
    539 
    540     fwrite($handle, $response['body']);
    541     fclose($handle);
     494        unlink( $tmpfname );
     495        return new WP_Error( 'http_404', trim( $response['response']['message'] ) );
     496    }
    542497
    543498    return $tmpfname;
Note: See TracChangeset for help on using the changeset viewer.