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;
}

At least WordPress, Drupal and probably most major CMS and frameworks have
this function or something equivalent.

This function is too trivial to ship as a separate package, but at the same
time, it's too error-prone and repetitive for every framework/project to
implement (and test) for itself... In my opinion, it's common enough that
it ought to just be built-in?


On Thu, Mar 30, 2017 at 5:45 PM, Kris Craig <kris.cr...@gmail.com> wrote:

>
> On Mar 30, 2017 8:21 AM, "Sara Golemon" <poll...@php.net> wrote:
> >
> > My first thought is UNC paths.  On windows a file server share is
> > denoted by \\host\share . if you combine that with relative paths
> > produced from PHP, you end up in the dubious situation of
> > "\\host\share/path/to/file" <--- wat?
> >
> > Overall, it smells of magic.
> >
> > -Sara
> >
> > On Thu, Mar 30, 2017 at 8:25 AM, Rasmus Schultz <ras...@mindplay.dk>
> wrote:
> > > Today, I ran into a very hard-to-debug problem, in which paths (to SQL
> > > files, in a database migration script) were kept in a map, persisted
> to a
> > > JSON file, and this file was moved from a Windows to a Linux
> file-system -
> > > because the paths on the Linux system had forward slashes, the files
> > > appeared to be missing from the map.
> > >
> > > Related questions are very commonly asked by Windows users, indicating
> that
> > > this is a common problem:
> > >
> > > http://stackoverflow.com/questions/14743548/php-on-
> windows-path-comes-up-with-backward-slash
> > > http://stackoverflow.com/questions/5642785/php-a-good-
> way-to-universalize-paths-across-oss-slash-directions
> > > http://stackoverflow.com/questions/6510468/is-there-a-
> way-to-force-php-on-windows-to-provide-paths-with-forward-slashes
> > >
> > > The answers that are usually given (use DIRECTORY_SEPARATOR, use
> > > str_replace() etc.) is that by default you automatically get
> cross-platform
> > > inconsistencies, and the workarounds end up complicating code
> everywhere,
> > > and sometimes lead to other (sometimes worse) portability problems.
> > >
> > > The problem is worsened by functions like glob() and the SPL
> directory/file
> > > traversal objects also producing inconsistent results.
> > >
> > > Returning backslashes on Windows seems rather unnecessary in the first
> > > place, since forward slashes work just fine?
> > >
> > > Might I suggest changing this behavior, such that file-system paths are
> > > consistently returned with a forward slash?
> > >
> > > Though this is more likely to fix rather than create issues, this
> could be
> > > a breaking change in some cases, so there should probably be an INI
> setting
> > > that enables the old behavior.
> > >
> > > Thoughts?
> >
> > --
> > PHP Internals - PHP Runtime Development Mailing List
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
>
> Another option would be to create a function that converts all slashes in
> a given input string to whatever the directory seperator should be on that
> platform.  This way, devs wouldn't have to deal with bulky aliases like
> DIRECTORY_SEPERATOR cluttering up their code.
>
> For example:
>
> <?php
>
> print convert_seperators( '/some\directory/' );
>
> ?>
>
> The above would output "/some/directory" on Linux and "\some\directory" on
> Windows.
>
> --Kris
>

Reply via email to