| 
        php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
  [2016-07-21 23:38 UTC] anrdaemon at freemail dot ru
 Description:
------------
realpath(), according to the documentation, is supposed to unwind every symlink and relative reference in the given string and produce an absolute and unambiguous path.
Well… it doesn't perform its job quite as desired.
Affected PHP versions: same behavior observed in 5.3.29, 5.6.23, 7.0.8 and 7.1.0b1.
Test script:
---------------
<?php
$rootdir = substr($_SERVER['SystemRoot'], 0, 3);
$tmpdir = getenv('TEMP');
shell_exec("mklink /J \"{$rootdir}home\" \"{$rootdir}Users\"");
shell_exec("mklink /J \"{$tmpdir}\\test.dir\" \"{$rootdir}home\\Public\\Desktop\"");
$file = "{$tmpdir}\\test.dir\\desktop.ini";
$rfile = realpath($file);
printf("orig.file name: %s\n", $file);
printf("realpath(name): %s\n", $rfile);
printf("THE real name:  %s\n", realpath($rfile));
if($rfile != realpath($rfile))
  print "Fail as is.\n";
rmdir("{$rootdir}home");
rmdir("{$tmpdir}\\test.dir");
Expected result:
----------------
orig.file name: C:\Users\ANRDAE~1\AppData\Local\Temp\test.dir\desktop.ini
realpath(name): C:\Users\Public\Desktop\desktop.ini
THE real name:  C:\Users\Public\Desktop\desktop.ini
Actual result:
--------------
orig.file name: C:\Users\ANRDAE~1\AppData\Local\Temp\test.dir\desktop.ini
realpath(name): C:\home\Public\Desktop\desktop.ini
THE real name:  C:\Users\Public\Desktop\desktop.ini
Fail as is.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             
             | 
    |||||||||||||||||||||||||||
            
                 
                Copyright © 2001-2025 The PHP GroupAll rights reserved.  | 
        Last updated: Tue Nov 04 02:00:01 2025 UTC | 
Did you ever read the provided test script? Did you try to run it? I can replace /J with /D, then you will not be able to run it without elevated privileges, but the result would not change. And it is funny you mentioned readlink, because 'realpath.php': <?php $rootdir = substr($_SERVER['SystemRoot'], 0, 3); $tmpdir = getenv('TEMP'); shell_exec("mklink /J \"{$rootdir}home\" \"{$rootdir}Users\""); shell_exec("mklink /J \"{$tmpdir}\\test.dir\" \"{$rootdir}home\\Public\\Desktop\""); $file = "{$tmpdir}\\test.dir\\desktop.ini"; $rfile = realpath($file); printf("orig.file name: %s\n", $file); printf("realpath(name): %s\n", $rfile); printf("THE real name: %s\n", realpath($rfile)); printf("readlink -fe: %s\n", shell_exec("readlink -fe \"$file\"")); if($rfile != realpath($rfile)) print "Fail as is.\n"; rmdir("{$rootdir}home"); rmdir("{$tmpdir}\\test.dir"); [C:\Programs\php-7.1.0b1]$ php.cmd -f realpath.php orig.file name: C:\Users\ANRDAE~1\AppData\Local\Temp\test.dir\desktop.ini realpath(name): C:\home\Public\Desktop\desktop.ini THE real name: C:\Users\Public\Desktop\desktop.ini readlink -fe: /c/Users/Public/Desktop/desktop.ini Fail as is. [C:\Programs\php-7.1.0b1]$ For your future genious resolutions, all NTFS symlinks are junctions, just with different attributes.