Automattic\WooCommerce\Internal\Utilities
FilesystemUtil::file_is_in_directory
Check if a given file is inside a given directory.
Method of the class: FilesystemUtil{}
No Hooks.
Returns
true|false
. True if the file is inside the directory.
Usage
$result = FilesystemUtil::file_is_in_directory( $file_path, $directory ): bool;
- $file_path(string) (required)
- The full path of the file to check.
- $directory(string) (required)
- The path of the directory to check.
FilesystemUtil::file_is_in_directory() FilesystemUtil::file is in directory code WC 9.9.4
private static function file_is_in_directory( string $file_path, string $directory ): bool { // Extract protocol if it exists. $protocol = ''; if ( preg_match( '#^([a-z0-9]+://)#i', $file_path, $matches ) ) { $protocol = $matches[1]; $file_path = preg_replace( '#^[a-z0-9]+://#i', '', $file_path ); } $file_path = (string) new URL( $file_path ); // This resolves '/../' sequences. $file_path = preg_replace( '/^file:\\/\\//', $protocol, $file_path ); $file_path = preg_replace( '/^file:\\/\\//', '', $file_path ); return 0 === stripos( wp_normalize_path( $file_path ), trailingslashit( wp_normalize_path( $directory ) ) ); }