Bug #68167 [Com]: Can't create a relative symlink without changing dir
| From: | nathan dot renniewaldock at gmail dot com | Date: | Wed, 08 Apr 2015 15:43:22 +0000 |
| Subject: | Bug #68167 [Com]: Can't create a relative symlink without changing dir | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-191902@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68167&edit=1
ID: 68167
Comment by: nathan dot renniewaldock at gmail dot com
Reported by: nathan dot renniewaldock at gmail dot com
Summary: Can't create a relative symlink without changing dir
Status: Not a bug
Type: Bug
Package: Filesystem function related
Operating System: Windows 7
PHP Version: 5.4.33
Block user comment: N
Private report: N
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2015-03-17 08:43:40] ab@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
Hi,
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.
Thanks.
------------------------------------------------------------------------
[2014-10-06 14:34:43] nathan dot renniewaldock at gmail dot com
Description:
------------
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:
---------------
<?php
file_put_contents("test.txt", time());
mkdir("a");
echo "symlink(): ";
symlink("..\\test.txt", "a\\1.txt") && print("success\n");
echo "\nmklink: ".shell_exec("mklink a\\2.txt ..\\test.txt");
echo "\nchdir(), symlink(): ";
chdir("a");
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]
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68167&edit=1