php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48547 DirectoryIterator Slash issue with getPathname Windows with Apache
Submitted: 2009-06-14 11:47 UTC Modified: 2009-09-06 14:57 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: BinaryKitten at jkrswebsolutions dot co dot uk Assigned: garretts (profile)
Status: Not a bug Package: SPL related
PHP Version: 5.2.9 OS: win32 only - WinXP SP3
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: BinaryKitten at jkrswebsolutions dot co dot uk
New email:
PHP Version: OS:

 

 [2009-06-14 11:47 UTC] BinaryKitten at jkrswebsolutions dot co dot uk
Description:
------------
When using the DirectoryIterator to go through files/folders on Windows under apache, the path has a mismatch of \ and /

Reproduce code:
---------------
<?php
$dir = new DirectoryIterator( $_SERVER['DOCUMENT_ROOT'] );
echo "<strong>".$dir->getPath()."</strong><br />";
foreach($dir as $file ) {
  $dirName = $file->getPathname();
  echo $dirName."<br />";
}
?>

Expected result:
----------------
With the Document root as C:\HTDOCS Apache returns $_SERVER['DOCUMENT_ROOT'] as c:/HTDOCS

Expected Output
C:/HTDOCS/.
C:/HTDOCS/..
C:/HTDOCS/css
C:/HTDOCS/index.php
C:/HTDOCS/js
C:/HTDOCS/

Actual result:
--------------
C:/HTDOCS\.
C:/HTDOCS\..
C:/HTDOCS\css
C:/HTDOCS\index.php
C:/HTDOCS\js


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-06-14 19:22 UTC] webmaster at asylum-et dot cm
I have tested this on Windows XP SP3 with PHP 5.2.5 and have the same 
findings as BinaryKitten at jkrswebsolutions dot co dot uk
 [2009-08-19 18:02 UTC] garretts@php.net
Use the FilesystemIterator class (a subclass of DirectorIterator) if you want to force the directory separator to the forward slash.

Directory iterator only supports the default platform slash.

http://us.php.net/manual/en/class.filesystemiterator.php

// will use forward slashes.
$dir = new FilesystemIterator( $path, 8192 );




 [2009-09-06 14:57 UTC] BinaryKitten at jkrswebsolutions dot co dot uk
FilesystemIterator is not available in the 5.2.9 codebase, 5.3+ so FilesystemIterator doesn't resolve the issue. What does is passing the $_SERVER['DOCUMENT_ROOT'] to realpath before passing the result to the DirectoryIterator;

<?php
$dir = new DirectoryIterator( realpath($_SERVER['DOCUMENT_ROOT']) );
echo "<strong>".$dir->getPath()."</strong><br />";
foreach($dir as $file ) {
  $dirName = $file->getPathname();
  echo $dirName."<br />";
}
?>

This "solves" the issue.
 [2012-12-22 10:12 UTC] vitoandre dot doria at gmail dot com
I really disagree with garretts "Not a bug" statement.

First of all if you had read the PHP Target Version you would know there is no 
FilesystemIterator in 5.2 Branch.

Secondly if the directoryIterator uses "the default platform slash" like you 
indicate then PHP or better the DirectoryIterator should:

A: treat the Path "c:/some/path/" as unresolvable as you state it doesnt use 
this slashes!
B: more likely convert all forward slashes on the target version to the "default 
platform slash" (you are doing this anyway for the 
last slash as you indicate which is clearly inconsistent)

Also have a note on: http://msdn.microsoft.com/en-
us/library/windows/desktop/aa365247(v=vs.85).aspx
It clearly states that the API would swap "ALL" forward slashes with backward 
slashes and not only the last one! Like 
directoryIterator does


BinaryKitten stated realpath would fix this issue which doesnt always apply as 
it might return null for the current path while 
DirectoryIterator returns a path (see: 
http://php.net/manual/en/function.realpath.php) which means this problem relys 
unresolved!

The "Not a bug" philosophy here on bugs.php.net in the last years is getting ill 
imho!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 05:01:29 2024 UTC