isBasePath.php
Table of Contents
Functions
- isBasePath() : bool
- Checks whether a given canonical path **lies inside** a base directory.
Functions
isBasePath()
Checks whether a given canonical path **lies inside** a base directory.
isBasePath(string $basePath, string $childPath) : bool
The comparison is done purely on canonicalized strings (see canonicalizePath()); the filesystem is not consulted.
Algorithm
- Canonicalize both
$basePath
and$ofPath
. - Right–trim the base (to avoid double slash issues).
- Append a trailing slash to both and use str_starts_with() to ensure
the whole first segment matches – preventing false positives like
/var/www-legacy
being considered inside/var/www
.
Parameters
- $basePath : string
-
The supposed ancestor directory.
- $childPath : string
-
The child path to test.
Tags
Return values
bool —True if childPath is equal to or contained in basePath.