Re: Directory separators on Windows

From: Date: Fri, 31 Mar 2017 09:40:42 +0000
Subject: Re: Directory separators on Windows
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-98698@lists.php.net to get a copy of this message
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

« previous php.internals (#98698) next »