Automattic\WooCommerce\Internal\Utilities

FilesystemUtil::validate_upload_file_pathpublic staticWC 1.0

Validate that a file path is a valid upload path.

Method of the class: FilesystemUtil{}

No Hooks.

Returns

null. Nothing (null).

Usage

$result = FilesystemUtil::validate_upload_file_path( $path ): void;
$path(string) (required)
The path to validate.

FilesystemUtil::validate_upload_file_path() code WC 9.8.5

public static function validate_upload_file_path( string $path ): void {
	$wp_filesystem = self::get_wp_filesystem();

	// File must exist and be readable.
	$is_valid_file = $wp_filesystem->is_readable( $path );

	// Check that file is within an allowed location.
	if ( $is_valid_file ) {
		$is_valid_file = self::file_is_in_directory( $path, $wp_filesystem->abspath() );
		if ( ! $is_valid_file ) {
			$upload_dir    = wp_get_upload_dir();
			$is_valid_file = false === $upload_dir['error'] && self::file_is_in_directory( $path, $upload_dir['basedir'] );
		}
	}

	if ( ! $is_valid_file ) {
		throw new \Exception( esc_html__( 'File path is not a valid upload path.', 'woocommerce' ) );
	}
}