php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61909 PHP realpath on Windows Case Issue
Submitted: 2012-05-02 17:37 UTC Modified: 2012-05-03 05:51 UTC
From: david at panmedia dot co dot nz Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: Irrelevant OS: Windows Vista/7
Private report: No CVE-ID: None
 [2012-05-02 17:37 UTC] david at panmedia dot co dot nz
Description:
------------
I have a symlink on my Windows server which was made like this:

F:\>mkdir link-target
F:\>mklink /D link f:\link-target 

(Note the lower case f: in the symlink target)

In PHP I run this:

$dir = realpath('f:\link');
var_dump($dir);

$dir = realpath($dir);
var_dump($dir);

Which outputs:

string 'f:\link-target' (length=14)
string 'F:\link-target' (length=14)

Notice the change in case on the second realpath.

The expected output is:

string 'F:\link-target' (length=14)
string 'F:\link-target' (length=14)

Test script:
---------------
<?php
// F:\>mkdir link-target
// F:\>mklink /D link f:\link-target 

$dir = realpath('f:\link');
var_dump($dir);

$dir = realpath($dir);
var_dump($dir);

Expected result:
----------------
string 'F:\link-target' (length=14)
string 'F:\link-target' (length=14)

Actual result:
--------------
string 'f:\link-target' (length=14)
string 'F:\link-target' (length=14)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-02 17:46 UTC] david at panmedia dot co dot nz
On further investigation I have noticed that on Windows symlinks are only followed 1 level deep:

// F:\>mkdir link-target
// F:\>mklink /D link f:\link-target 
// F:\>mklink /D link2 f:\link

$dir = realpath('f:\link2');
var_dump($dir);

$dir = realpath($dir);
var_dump($dir);

$dir = realpath($dir);
var_dump($dir);

// string 'f:\link' (length=7)
// string 'f:\link-target' (length=14)
// string 'F:\link-target' (length=14)
 [2012-05-03 03:10 UTC] laruence@php.net
path in windows is case insensitive..
 [2012-05-03 03:16 UTC] david at panmedia dot co dot nz
@laruence@php.net

Well, not always. In Windows Server 2008 it can be configured. 
http://technet.microsoft.com/en-us/library/cc725747.aspx

Also, the point of realpath is to return the canonicalized absolute pathname, but it does not. Especially in the case of nested symlinks, as I pointed out in my other comment.

You should be able to test if a path is the same by going

if (realpath('c:\path-to-link') === realpath('C:\RealPathToTarget')) ...
 [2012-05-03 05:51 UTC] pajoye@php.net
-Status: Open +Status: Not a bug
 [2012-05-03 05:51 UTC] pajoye@php.net
We do not support case sensitive paths on windows, so do 99.999% of the windows 
apps as well.

strcasecmp is what you actually need.
 [2012-05-03 21:19 UTC] david at panmedia dot co dot nz
@pajoye, you missed my point about realpath not resolving the correct link target if the case is different. See my first comment.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Sep 19 15:01:30 2019 UTC