Make WordPress Core

Changeset 52701


Ignore:
Timestamp:
02/11/2022 03:48:44 PM (3 years ago)
Author:
SergeyBiryukov
Message:

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

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 [50810], [52696], [52698].

Props maxkellermann.
See #55069.

File:
1 edited

Legend:

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

    r52698 r52701  
    32663266        }
    32673267
    3268         $handle = fopen( $file, 'rb' );
    3269         if ( false === $handle ) {
    3270             return false;
    3271         }
    3272 
    3273         $magic = fread( $handle, 12 );
     3268        $magic = file_get_contents( $file, false, null, 0, 12 );
     3269
    32743270        if ( false === $magic ) {
    32753271            return false;
     
    32903286            $mime = 'image/webp';
    32913287        }
    3292 
    3293         fclose( $handle );
    32943288    } catch ( Exception $e ) {
    32953289        $mime = false;
Note: See TracChangeset for help on using the changeset viewer.