php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72582 RecursiveDirectoryIterator with UNIX_PATHS flag doesn't work
Submitted: 2016-07-12 08:20 UTC Modified: 2016-07-12 11:54 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:2 (100.0%)
From: rasmus at mindplay dot dk Assigned:
Status: Verified Package: SPL related
PHP Version: 7.0.8 OS: Windows
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: rasmus at mindplay dot dk
New email:
PHP Version: OS:

 

 [2016-07-12 08:20 UTC] rasmus at mindplay dot dk
Description:
------------
The UNIX_PATHS flags, according to documentation, "Makes paths use Unix-style forward slash irrespective of system default".

This is important e.g. for the purposes of comparing paths, but it doesn't seem to have any effect on Windows. The paths have backslashes regardless of setting this flag.

I also tried setting the CURRENT_AS_PATHNAME flag, since I suspected this was due to the fact that iterators etc. likely *can't* impose this change on SplFileInfo instances in the first place? Even if they could, that would make the behavior of SplFileInfo::getPathname() somewhat unpredictable.

I'm currently forced to do something like $path = strtr('\\', '/', $path) in every program that compares paths, to make sure it works on Windows, which is really messy.

(I often find myself wondering why backslashes are used in paths on Windows at all? Since forward slashes work just fine. One usually cares more about paths being comparable, e.g. actually working and behaving predictably and consistently, than what it looks like when printed on screen...)


Test script:
---------------
<?php

$files = new RecursiveDirectoryIterator(
    __DIR__, // (or somewhere specific)
    RecursiveDirectoryIterator::SKIP_DOTS | RecursiveDirectoryIterator::UNIX_PATHS
);

foreach ($files as $file) {
    var_dump($file->getPathname()); // paths with backslashes :-(
}


Expected result:
----------------
UNIX-style forward slashes, per the description of the flag.

Actual result:
--------------
Mixed Windows/UNIX-style backslashes and forward slashes.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-07-12 11:54 UTC] cmb@php.net
-Status: Open +Status: Verified -Package: SPL_Types +Package: SPL related
 [2016-07-12 11:54 UTC] cmb@php.net
I can confirm this behavior. I'm not sure, whether this qualifies
as implementation or a documentation bug, though. Of course, the
mixed directory separators are usually not desired, but still the
implementation would make some sense, as only the given $path will
contain Windows directory separators, but not the rest, cf.

    <?php
    $files = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator(
            __DIR__,
            RecursiveDirectoryIterator::SKIP_DOTS | RecursiveDirectoryIterator::UNIX_PATHS
        )
    );
    foreach ($files as $file) {
        var_dump($file->getPathname()); // paths with mixed dir separators
    }

> I often find myself wondering why backslashes are used in paths
> on Windows at all?

Consider exec(), for instance.
 [2016-07-12 15:17 UTC] rasmus at mindplay dot dk
> Of course, the mixed directory separators are usually not desired

I think it's fair to say, they're never desired - you want either UNIX or Windows paths, there's no scenario (least of all printing to display) where having a mix isn't just "weird".

> but still the
> implementation would make some sense, as only the given $path will
> contain Windows directory separators, but not the rest, cf.

I don't think that makes sense. You'd have to update the documentation to explain that it only normalizes the sub-paths - but that would be completely pointless; if I have to normalize the base path myself, that means I have to normalize paths myself, which means I might as well just normalize all the paths myself. As is, this feature suggests I wouldn't need to, but that's not true.

What possible use would anyone have for a partially normalized path?

I don't see how this is not a bug.
 [2016-07-13 17:19 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=339658
Log: Document #72582: RecursiveDirectoryIterator with UNIX_PATHS flag doesn't work
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC