Make WordPress Core

Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#46487 closed defect (bug) (duplicate)

wp_delete_file_from_directory doesn't work with stream wrappers

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

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 8 years ago.
46487-1.diff​ (853 bytes ) - added by vtemian 8 years ago.

Download all attachments as: .zip

Change History (9)

#1 @vtemian
8 years ago

  • Keywords has-patch added

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


8 years ago

#3 @vtemian
8 years ago

  • Component Filesystem API → General

#4 @SergeyBiryukov
8 years ago

  • Component General → Filesystem API
  • Milestone Awaiting Review → 5.3

#5 @SergeyBiryukov
8 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
8 years ago

  • Resolution → worksforme
  • Status new → closed

It works now, thanks!

#7 @SergeyBiryukov
7 years ago

  • Milestone 5.3
  • Resolution worksforme → duplicate

Duplicate of #44563.

Thanks for the follow-up!

Note: See TracTickets for help on using tickets.