2

If I have a directory structure like

source/ | file.php | subdir/ | | text.txt ("Wrong Text")

Directory1/ | test1.php -> source/file.php | subdir/ | | text.txt ("Right Text 1")

Directory2/ | -test2.php -> source/file.php | subdir/ | | text.txt ("Right Text 2")

And file.php looks in subdir and displays the contents of text.txt, what's happening is I'm always getting "Wrong Text" displaying because the it's resolving the relative path for subdir from the symlink's target rather than the symlink itself.

I'm not sure if this is from Apache or PHP. Is there any way to make it work such that when it executes, relative paths look at the symlink itself rather than the target of the link so I'd get "Right Text 1" and "Right Text 2" from the output?

Trel
  • 323
  • 4
  • 15
  • Do test1.php and test2.php `require '../source/file.php';`? Can you post the source to file.php so we can see what's doing? (Or a simplified version that exhibits the problem.) – Matt Feb 18 '16 at 17:36
  • test1.php and test2.php ARE ../source/file.php. They're symlinks to file.php. And file.php for the purposes of this question is `` – Trel Feb 18 '16 at 18:02
  • Ah, I see. The simplest way to fix it would be to create actual files that were empty shells that required the original without using symlinks. Then they would read the files that were relative. – Matt Feb 18 '16 at 18:07
  • Unfortunately that's not a possibility here. My ultimate goal is I'm trying to make a secondary install of a helpdesk software (including customizations I made) at a second location except using a different config file. What I tried to do was symlink all files and directories except the config file which had the instance and DB information. However, when I used symlinks, it would always ALSO load the config file from the target's location rather than the local one. – Trel Feb 18 '16 at 22:29
  • How are you loading `test.txt` from `file.php`? – Matt Feb 18 '16 at 22:41
  • Do this exactly and it'll replicate the issue I'm having `` (The script I'm using (and trying to make a copy of with a single set of files (save the config) define's it's include directory (where the config file is) with dirname(\_\_FILE__)). – Trel Feb 19 '16 at 15:06

1 Answers1

2

Modified slightly: PHP dirname returns symlink path

if (is_link(__FILE__)) {
    echo file_get_contents(readlink(dirname(__FILE__))."/subdir/file.txt");
}else{
    echo file_get_contents(dirname(__FILE__)."/subdir/file.txt");
}
Community
  • 1
  • 1
Matt
  • 5,315
  • 1
  • 30
  • 57
  • Well, since the whole purpose of what I'm trying to do is to run two instances of one set of files including modifications, this (another modification) looks like it'll probably be the best option. (Though I'll need to modify it a bit, since the file itself is not a link, the folder is linked, so `is_link()` does not return true. EDIT: Though if I symlink the folder contents rather than the folder, that could work). – Trel Feb 19 '16 at 16:01
  • Unfortunately, due to the fact that in the bulk of the case, I'm symlinking the directory itself, it seems like the best option is going to be to NOT use \_\_FILE\_\_ at all and use an alternate method to resolve the base path of the instance. – Trel Feb 23 '16 at 15:26