Make WordPress Core

Changeset 53860


Ignore:
Timestamp:
08/08/2022 12:39:51 PM (21 months ago)
Author:
audrasjb
Message:

Filesystem: Rewrite FTP/FTP Sockets exists() methods to implement a more stable check.

WordPress FTP file checking was previously based upon ftp_nlist(). This function can be problematic at scale with a directory containing a large number of files. The same issue occurred using it with ftpsockets.

This changeset rewrites the FTP exists() functions to utilize a more efficient and stable check.

Props giox069, desrosj, mkox, afragen, costdev, pbiron, peterwilsoncc.
Fixes #51170.
See #53318, #39781.

Location:
trunk/src/wp-admin/includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-filesystem-ftpext.php

    r53714 r53860  
    413413     *
    414414     * @since 2.5.0
     415     * @since 6.1.0 Rewrite using ftp_rawlist, uses 'LIST' on FTP server
     416     *              takes file path or directory path as parameter.
    415417     *
    416418     * @param string $file Path to file or directory.
     
    418420     */
    419421    public function exists( $file ) {
    420         $list = ftp_nlist( $this->link, $file );
    421 
    422         if ( empty( $list ) && $this->is_dir( $file ) ) {
    423             return true; // File is an empty directory.
    424         }
    425 
    426         return ! empty( $list ); // Empty list = no file, so invert.
     422        if ( $this->is_dir( $file ) ) {
     423            return true;
     424        }
     425
     426        return ! empty( ftp_rawlist( $this->link, $file ) );
    427427    }
    428428
     
    511511     *
    512512     * @since 2.5.0
     513     * @since 6.1.0 Update for proper return values.
    513514     *
    514515     * @param string $file Path to file.
    515      * @return int|false Size of the file in bytes on success, false on failure.
     516     * @return int Size of the file in bytes on success, -1 on failure.
    516517     */
    517518    public function size( $file ) {
  • trunk/src/wp-admin/includes/class-wp-filesystem-ftpsockets.php

    r52332 r53860  
    415415     *
    416416     * @since 2.5.0
     417     * @since 6.1.0 Rewrite using file size.
    417418     *
    418419     * @param string $file Path to file or directory.
     
    420421     */
    421422    public function exists( $file ) {
    422         $list = $this->ftp->nlist( $file );
    423 
    424         if ( empty( $list ) && $this->is_dir( $file ) ) {
    425             return true; // File is an empty directory.
    426         }
    427 
    428         return ! empty( $list ); // Empty list = no file, so invert.
    429         // Return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server.
     423        if ( $this->is_dir( $file ) ) {
     424            return true;
     425        }
     426
     427        return is_numeric( $this->size( $file ) );
    430428    }
    431429
Note: See TracChangeset for help on using the changeset viewer.