|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68167 Can't create a relative symlink without changing dir
Submitted: 2014-10-06 14:34 UTC Modified: 2015-03-17 08:43 UTC
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: nathan dot renniewaldock at gmail dot com Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 5.4.33 OS: Windows 7
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
47 - 37 = ?
Subscribe to this entry?

 [2014-10-06 14:34 UTC] nathan dot renniewaldock at gmail dot com
symlink() fails to create a relative link if the working dir is not the same as where the link is going (works correctly on Linux). Relative targets should be resolved from the dir the link will be. For now, my workaround is calling mklink through shell_exec.

Test script:
file_put_contents("test.txt", time());


echo "symlink(): ";
symlink("..\\test.txt", "a\\1.txt") && print("success\n");

echo "\nmklink: ".shell_exec("mklink a\\2.txt ..\\test.txt");

echo "\nchdir(), symlink(): ";
symlink("..\\test.txt", "3.txt") && print("success\n");

Actual result:
C:\php-test>php a.php
symlink(): PHP Warning:  symlink(): Could not fetch file information(error 2) in C:\php-test\a.php on line 7

Warning: symlink(): Could not fetch file information(error 2) in C:\php-test\a.php on line 7

mklink: symbolic link created for a\2.txt <<===>> ..\test.txt

chdir(), symlink(): success

C:\php-test>dir a
 Directory of C:\php-test\a
06/10/2014  15:21    <SYMLINK>      2.txt [..\test.txt]
06/10/2014  15:21    <SYMLINK>      3.txt [..\test.txt]


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-03-17 08:43 UTC]
-Status: Open +Status: Not a bug
 [2015-03-17 08:43 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at


thanks for the report. In general - you're right. But. chdir() is not thread safe. The most PHP builds on Linux are NTS, so that doesn't matter. However as soon as you're on the TS build, another thread could have changed CWD right before you symlink with a relative path. Then it will suddenly fail being not in CWD expected. Thus - this is not portable and should not be used this way.

 [2015-04-08 15:43 UTC] nathan dot renniewaldock at gmail dot com
You appear to have misunderstood the report.
Attempting to create a relative link without the working directory being where the link is being placed will always fail because it's resolving the target file from the working directory, not the directory the link will be in (which is how Windows resolves them).

From my example, symlink() is trying to find: $CWD\..\test.txt, not $CWD\a\..\test.txt
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon Nov 29 12:03:14 2021 UTC