Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 5 years ago

#46487 closed defect (bug) (duplicate)

wp_delete_file_from_directory doesn't work with stream wrappers

Reported by: vtemian's profile vtemian Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Filesystem API Keywords: has-patch
Focuses: Cc:

Description

Since PHP realpath doesn't support stream wrappers, you won't be able to delete a file that uses stream wrappers.

We can check if the file is using stream wrappers using the wp_is_stream( $file ) and avoid parsing the realpath.

<?php
function wp_delete_file_from_directory( $file, $directory ) {
        $file_path      = wp_normalize_path( $file ) ;
        $directory_path = wp_normalize_path( $directory ) ;

        if ( ! wp_is_stream( $file ) ) {
                $real_file      = realpath( $file_path );
                $real_directory = realpath( $directory_path );

                if ( false === $real_file || false === $real_directory || strpos( wp_normalize_path( $real_file ), trailingslashit( wp_normalize_path( $real_directory ) ) ) !== 0 ) {
                        return false;
                }
        }

        if ( strpos( $file_path, trailingslashit( $directory_path ) ) !== 0 ) {
                return false;
        }

        wp_delete_file( $file );

        return true;

Attachments (2)

46487.diff (1.1 KB) - added by vtemian 6 years ago.
46487-1.diff (853 bytes) - added by vtemian 6 years ago.

Download all attachments as: .zip

Change History (9)

@vtemian
6 years ago

#1 @vtemian
6 years ago

  • Keywords has-patch added

@vtemian
6 years ago

This ticket was mentioned in Slack in #core by vtemian. View the logs.


6 years ago

#3 @vtemian
6 years ago

  • Component changed from Filesystem API to General

#4 @SergeyBiryukov
6 years ago

  • Component changed from General to Filesystem API
  • Milestone changed from Awaiting Review to 5.3

#5 @SergeyBiryukov
6 years ago

This appears to be a duplicate of #44563, now fixed in [45177].

@vtemian, could you test the changes made in [45177]?

#6 @vtemian
6 years ago

  • Resolution set to worksforme
  • Status changed from new to closed

It works now, thanks!

#7 @SergeyBiryukov
5 years ago

  • Milestone 5.3 deleted
  • Resolution changed from worksforme to duplicate

Duplicate of #44563.

Thanks for the follow-up!

Note: See TracTickets for help on using tickets.