Trel
Trel

Reputation: 353

How to make PHP/Apache use symlinks relative to the link not the target

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?

Upvotes: 2

Views: 2176

Answers (1)

Matt
Matt

Reputation: 5428

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");
}

Upvotes: 2

Related Questions