Bug #68167 [Opn->Nab]: Can't create a relative symlink without changing dir
Edit report at https://bugs.php.net/bug.php?id=68167&edit=1
ID: 68167
Updated by: ab@php.net
Reported by: nathan dot renniewaldock at gmail dot com
Summary: Can't create a relative symlink without changing dir
-Status: Open
+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:
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.
Previous Comments:
------------------------------------------------------------------------
[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
Thread (3 messages)