path
Table of Contents
Functions
- canonicalizePath() : string
- Converts any given path to a **canonical, absolute‑style** representation.
- computeRelativePath() : string
- Computes the relative path between two normalized relative paths.
- directoryPath() : string
- Returns the directory part of the path.
- extractCanonicalParts() : array<string|int, string>
- Extracts the canonical path segments of « $pathWithoutRoot ».
- isAbsolutePath() : bool
- Determines whether a given path is absolute.
- isBasePath() : bool
- Checks whether a given canonical path **lies inside** a base directory.
- isLocalPath() : bool
- Determines whether the given path refers to a local file system location.
- isRelativePath() : bool
- Determines whether a given path is relative.
- joinPaths() : string
- Concatenates multiple path fragments into a single canonical path.
- normalizePath() : string
- Normalizes the given file system path by replacing backslashes with slashes.
- relativePath() : string
- Returns the relative path from a base path to a target path.
- splitPath() : array<string|int, string>
- Splits a **canonical** filesystem path into two parts: 1. **Root** : the protocol / drive / leading slash portion 2. **Remainder** : the sub‑path that follows
Functions
canonicalizePath()
Converts any given path to a **canonical, absolute‑style** representation.
canonicalizePath(string $path) : string
The algorithm:
- Early return / cache – looks up the path in a static LRU‑style buffer (see CanonicalizeBuffer).
- Home expansion – replaces a leading tilde
~
with the current user’s home directory (platform‑aware via getHomeDirectory()). - Separator normalisation – back‑slashes ⇒ forward‑slashes via normalizePath().
- Root / remainder split – handled by splitPath().
- Dot & dot‑dot cleanup – collapses
.
and..
segments with extractCanonicalParts(). - Buffer write‑back – stores the result; periodically cleans the buffer after insertions.
No filesystem access is performed; non‑existent paths are allowed.
Parameters
- $path : string
-
The path to canonicalise. May be relative or absolute, Windows or Unix, and may start with
~
.
Tags
Return values
string —Canonicalised path with forward slashes and no redundant
.
/ ..
segments.
computeRelativePath()
Computes the relative path between two normalized relative paths.
computeRelativePath(string $targetPath, string $basePath) : string
Parameters
- $targetPath : string
-
The target relative path.
- $basePath : string
-
The base relative path.
Tags
Return values
string —The relative path from base to target.
directoryPath()
Returns the directory part of the path.
directoryPath(string $path) : string
This function normalizes and extracts the parent directory from a file path. It handles edge cases not covered by PHP's built-in dirname(), such as:
- Preserving trailing slashes in Windows root paths (e.g., "C:/")
- Correctly handling URI schemes (e.g., "file:///path/to/file")
- Supporting UNC (network) paths on Windows (e.g., "\server\share\folder")
- Returning an empty string when no directory is applicable
This method is similar to PHP's dirname(), but handles various cases where dirname() returns a weird result:
- dirname() does not accept backslashes on UNIX
- dirname("C:/symfony") returns "C:", not "C:/"
- dirname("C:/") returns ".", not "C:/"
- dirname("C:") returns ".", not "C:/"
- dirname("symfony") returns ".", not ""
- dirname() does not canonicalize the result
This method fixes these shortcomings and behaves like dirname() otherwise.
The result is a canonical path.
Parameters
- $path : string
-
The file path from which to extract the directory.
Tags
Return values
string —The directory part of the path, or an empty string if no directory can be extracted.
extractCanonicalParts()
Extracts the canonical path segments of « $pathWithoutRoot ».
extractCanonicalParts(string $root, string $pathWithoutRoot) : array<string|int, string>
This method breaks down the input path into segments, and processes them to remove:
- Redundant current directory references (
.
) - Properly resolvable parent references (
..
)
It does not access the filesystem and works purely on string logic.
Parameters
- $root : string
-
The base root path. Used to determine whether
..
segments can be collapsed. If empty, leading..
segments will be preserved. - $pathWithoutRoot : string
-
The input path (already stripped of the root part).
Tags
Return values
array<string|int, string> —An array of canonical path segments.
isAbsolutePath()
Determines whether a given path is absolute.
isAbsolutePath(string $path) : bool
This function checks for both UNIX-style absolute paths (starting with "/") and Windows-style absolute paths (e.g., "C:", "C:/", or just "C:"). It also handles paths with URI schemes like "file://" by stripping the scheme before analysis.
Parameters
- $path : string
-
The path to check.
Tags
Return values
bool —True if the path is absolute, false otherwise.
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.
isLocalPath()
Determines whether the given path refers to a local file system location.
isLocalPath(string $path) : bool
A path is considered "local" if:
- It is not empty.
- It does not contain a URL-like scheme (e.g.
http://
,ftp://
,s3://
, etc.).
This function uses a lightweight string check to detect the presence of ://
,
which is common in remote paths or stream wrappers.
Parameters
- $path : string
-
The path to check. Can be relative, absolute, or URL-style.
Tags
Return values
bool —True if the path is local; false if it looks like a remote or virtual path.
isRelativePath()
Determines whether a given path is relative.
isRelativePath(string $path) : bool
A path is considered relative if it is not absolute. This function is the
direct inverse of isAbsolutePath()
. It will return true for paths that
do not start with a slash, a backslash, or a Windows drive letter.
Parameters
- $path : string
-
The path to check.
Tags
Return values
bool —True if the path is relative, false if it is absolute.
joinPaths()
Concatenates multiple path fragments into a single canonical path.
joinPaths(string ...$paths) : string
Behaviour rules
- Empty segments (
''
) are ignored. - The first non‑empty segment is kept “as‑is” so a leading slash, drive
letter (
C:/
), or scheme (phar://
) is preserved. - Every subsequent segment is joined with exactly one forward‑slash
(
/
) separator – unless the previous fragment already ends with/
or\
. - After assembly, the result is passed through canonicalizePath() to
normalise slashes and collapse
.
/..
. - If all fragments are empty, an empty string is returned.
⚠ Type‑hint fix – the function returns a string, not a bool. The signature has been updated accordingly.
Parameters
- $paths : string
-
Arbitrary number of path fragments. May contain Unix, Windows or URL‑style segments.
Tags
Return values
string —Canonical, joined path (or empty string).
normalizePath()
Normalizes the given file system path by replacing backslashes with slashes.
normalizePath(string $path) : string
This function is useful to unify path separators across different operating systems.
It ensures all directory separators are forward slashes (/
), making the path
suitable for consistent comparison, storage, or manipulation.
It does not resolve relative components like .
or ..
, nor does it
canonicalize or validate the path against the filesystem.
Parameters
- $path : string
-
The path to normalize. Can contain mixed or inconsistent separators.
Tags
Return values
string —The normalized path with all backslashes (\
) replaced by forward slashes (/
).
relativePath()
Returns the relative path from a base path to a target path.
relativePath(string $path, string $basePath) : string
Both paths must be of the same type (both absolute or both relative), and must share the same root (e.g., same drive letter on Windows). The result is a canonical relative path.
Parameters
- $path : string
-
The target path.
- $basePath : string
-
The base path.
Tags
Return values
string —The relative path.
splitPath()
Splits a **canonical** filesystem path into two parts: 1. **Root** : the protocol / drive / leading slash portion 2. **Remainder** : the sub‑path that follows
splitPath(string $path) : array<string|int, string>
Supported patterns
Input example | Returned root | Returned remainder |
---|---|---|
/var/www/html |
/ |
var/www/html |
C:/Windows/System32 |
C:/ |
Windows/System32 |
C: |
C:/ |
(empty) |
file:///home/user/docs |
file:/// |
home/user/docs |
\\\\server\\share\\folder (UNC) |
// |
server/share/folder |
Note : The input path is assumed to be canonical (slashes normalisés à
/
, pas de segments.
ou..
). The function does not perform any filesystem checks.
Parameters
- $path : string
-
Canonical path (absolute or relative, UNIX or Windows, with optional URL‑style scheme).
Tags
Return values
array<string|int, string> —An array with [0] => root, [1] => remainder. Both strings can be empty.