webmozart/path-util
Lightweight PHP utility for safe, cross-platform path handling. Normalize, join, resolve and compare filesystem paths, with helpers for absolute/relative paths and canonicalization. Useful for file operations and libraries needing consistent path logic.
C:\foo\bar ↔ /foo/bar), which is critical for Laravel applications deployed across Windows/Linux/macOS environments (e.g., shared hosting, Docker, CI/CD pipelines). It aligns with Laravel’s filesystem abstraction but offers finer-grained control for edge cases (e.g., symlinks, trailing slashes).Storage facade handles path resolution, this package could enhance:
realpath(), DIRECTORY_SEPARATOR, etc.vendor/ via Composer (if maintained or forked).../ traversal, mixed separators).Storage::put()).str() helpers or Illuminate\Support\Str may suffice for basic cases, but this package offers path-specific logic (e.g., isAbsolute(), normalize()).realpath() changes, type safety).pestphp).PathUtil::normalize()) if the package is critical.str_replace() + DIRECTORY_SEPARATOR for simple cases.spatie/path-util) or similar packages (e.g., symfony/filesystem).Filesystem, Storage, and Http components.Str::of(): Limited to string manipulation (no path-specific logic).rtrim($path, '/') . DIRECTORY_SEPARATOR for simple cases.Storage::path(), file_put_contents()).../ traversal).storage_path() integration).// app/Helpers/PathHelper.php
use function Webmozart\PathUtil\normalize;
function laravelSafePath(string $path): string {
return normalize($path, '/'); // Force Unix-style for Laravel
}
Storage/Filesystem calls with normalized paths.realpath(), str_contains(), etc.).create_function).\\server\share).Storage facade.Filesystem API shifts).PATH_NORMALIZATION section to the team’s architecture docs.## Path Handling
- Always use `PathHelper::normalize()` for filesystem operations.
- Avoid hardcoding `DIRECTORY_SEPARATOR`; use `DIRECTORY_SEPARATOR` constant.
| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Package fork breaks on PHP 8.2 | Path operations fail silently. | Roll back to last known good version. |
| Cross-platform path mismatch | Uploads fail or corrupt on Windows. | Add pre-flight path validation. |
| Symlink resolution issues | Security risks (e.g., directory traversal). | Use realpath() with safe defaults. |
| CI/CD flakiness | Tests pass locally but fail in pipeline. | Use matrix testing (Windows/Linux). |
../, trailing slashes).// DO:
$safePath = PathHelper::normalize($userInput);
// DON'T:
$unsafePath = __DIR__ . $userInput; // Vulnerable to traversal!
How can I help you explore Laravel packages today?