php.net |  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
Votes:1
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
 [2014-10-06 14:34 UTC] 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]


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-03-17 08:43 UTC] ab@php.net
-Status: Open +Status: Not a bug
 [2015-03-17 08:43 UTC] 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.
 [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-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 01:01:28 2024 UTC