-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Filesystem] Document the Path::join()
method
#21164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Path::join()
method
Path::join()
methodPath::join()
method
fa81e3d
to
0ef903d
Compare
- Add clear documentation for path joining behavior - Show examples for common use cases - Clarify that leading slashes in subsequent arguments are removed - Include examples for empty parts, trailing slashes, and multiple arguments
0ef903d
to
9ef642b
Compare
// => /var/www/vhost/config.ini | ||
|
||
echo Path::join('C:\\Program Files', 'PHP', 'php.ini'); | ||
// => C:/Program Files/PHP/php.ini |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks correct because the join()
method calls canonicalize()
and this turns any slashes into forward slashes. See https://github.com/symfony/symfony/blob/01f0faf9c332bda3d63e4dce05e4deaf8491cf2b/src/Symfony/Component/Filesystem/Path.php#L49-L66
My question though: is the resulting path fully valid on Windows?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the resulting path is fully valid on Windows. Windows accepts both forward slashes (/) and backslashes () as path separators in most contexts, including:
- File operations in PHP (fopen, file_get_contents, etc.)
- Command line operations
- Most Windows APIs
The canonicalization to forward slashes is actually a best practice for cross-platform compatibility. PHP's file system functions handle forward slashes correctly on Windows, automatically converting them when needed for the underlying Windows API calls.
The only exception might be when passing paths to external Windows programs that specifically require backslashes, but this is rare and can be handled with Path::normalize() when needed.
Here are some references:
-
PHP Manual on Windows file paths: https://www.php.net/manual/en/wrappers.file.php
"On Windows, both slash (/) and backslash () are used as directory separator character."
-
Microsoft Documentation: https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file
"File I/O functions in the Windows API convert "/" to "" as part of converting the name to an NT-style name"
-
PHP source code (main/streams/plain_wrapper.c): PHP internally handles the conversion when needed for Windows file operations.
You can test this yourself on Windows:
// Both work identically on Windows:
file_exists("C:/Windows/System32"); // true
file_exists("C:\\Windows\\System32"); // true
Thanks Oskar! I did some mionor tweakes while merging (6276aca) to mention that both types of slashes work on Windows. |
Fix #21163