Make WordPress Core

Changeset 52698


Ignore:
Timestamp:
02/10/2022 03:01:06 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Use file_get_contents() in get_file_data().

file_get_contents() is faster than fread(), because the PHP core can decide how to best read the remaining file; it could decide to issue just one read() call or mmap() the file first.

Per the PHP manual, file_get_contents() or stream_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by the OS to enhance performance.

Reference: PHP Manual: file_get_contents().

Follow-up to [12044], [49073], [52696].

Props maxkellermann.
See #55069.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r52640 r52698  
    65676567 */
    65686568function get_file_data( $file, $default_headers, $context = '' ) {
    6569     // We don't need to write to the file, so just open for reading.
    6570     $fp = fopen( $file, 'r' );
    6571 
    6572     if ( $fp ) {
    6573         // Pull only the first 8 KB of the file in.
    6574         $file_data = fread( $fp, 8 * KB_IN_BYTES );
    6575 
    6576         // PHP will close file handle, but we are good citizens.
    6577         fclose( $fp );
    6578     } else {
     6569    // Pull only the first 8 KB of the file in.
     6570    $file_data = file_get_contents( $file, false, null, 0, 8 * KB_IN_BYTES );
     6571
     6572    if ( false === $file_data ) {
    65796573        $file_data = '';
    65806574    }
Note: See TracChangeset for help on using the changeset viewer.