Re: Directory separators on Windows
| From: | Rasmus Schultz | Date: | Sat, 01 Apr 2017 09:13:24 +0000 |
| Subject: | Re: Directory separators on Windows | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-98713@lists.php.net to get a copy of this message | ||
> Also ucfirst is useless (or any case operations)
It's not useless, if you want a normalized path on Windows, it has to
include a drive-letter, and Windows FS isn't case-sensitive.
> Right now realpath will fail if the path does not exist
I know, that's one reason I don't use it.
It kind of solves a different problem, e.g. resolves ".." and "." elements
in paths... as a rule, I don't ever use relative paths, but it would
certainly be nice to have a realpath() that works for files that haven't
been created yet.
I don't think you can simply make realpath() also normalize the path, as
this would be a breaking change?
I guess an improved realpath() could be used internally as part of a
normalize_path() function, but it's not enough on it's own, since the real
path will still have platform-specific directory-separators, so a
normalize_path() function would still be useful if realpath() gets improved.
So to summarize, a normalize_path() function should:
1. Fully normalize to an absolute path with no platform-specific separators
2. Have corrected case (for files/dirs that do exist.)
3. Have normalized (upper-case) drive-letter on Windows
There's also network file-system paths on Windows with a different syntax
to consider? I don't know much about that...
On Fri, Mar 31, 2017 at 11:40 AM, Pierre Joye <pierre.php@gmail.com> wrote:
> On Fri, Mar 31, 2017 at 3:32 PM, Rasmus Schultz <rasmus@mindplay.dk>
> wrote:
> > Well, this is the opposite of what I'm asking for, and does not address
> the
> > case where paths have been persisted in a file or database and the data
> > gets accessed from different OS.
> >
> > I understand the reasons given for not changing this behavior in PHP
> > itself, so maybe we could have a standard function that normalizes paths
> to
> > forward slashes? e.g. basically:
> >
> > /**
> > * Normalize a filesystem path.
> > *
> > * On windows systems, replaces backslashes with forward slashes
> > * and ensures drive-letter in upper-case.
> > *
> > * @param string $path
> > *
> > * @return string normalized path
> > */
> > function normalize_path( $path ) {
> > $path = str_replace('\\', '/', $path);
> >
> > return $path{1} === ':'
> > ? ucfirst($path)
> > : $path;
> > }
>
> Also ucfirst is useless (or any case operations). realpath goes
> further down by solving ugly things like \\\\\\\ or ////// (code
> concatenating paths without checking trailing /\.
>
> > At least WordPress, Drupal and probably most major CMS and frameworks
> have
> > this function or something equivalent. .
>
> Now I remember why they have to do that.
>
> realpath is not fully exposed in userland. virtual_file_ex should be
> used and provide the option to validate path or not. Right now
> realpath will fail if the path does not exist. I would suggest to
> expose this functionality/option and that will solve the need to
> implement such things in userland.
>
> ps: I discussed that long time with Dmitry and forgot to implement it,
> I take the blame for not having that in 7.x :)
>
> Cheers,
> Pierre
>