php - Obtaining the current working directory of a symbolic link

I have an Apache server which serves one site, say example.com, at/var/www/html/
, and another site, say othersite.com, at/home/michael/public_html/
. I have a symbolic link in the latter directory, which points to a file in the former. Here's an example directory structure, to give some context for what I'm looking to do.
/var/www/html/:
header.php
a.php
mydir/:
b.php
/home/michael/public_html/:
header.php -> /var/www/html/header.php
a.php
mydir/:
b.php
header.php
includes the linerequire('a.php');
. What I'm expecting is, if I'm visiting example.com, thisrequire
will load in the file at/var/www/html/a.php
, but if I'm on othersite.com, it instead will load/home/michael/public_html/a.php
.
For extra clarity, let's say those twoa.php
files have the following contents:
/var/www/html/a.php:
echo("I'm on example.com");
/home/michael/public_html/a.php:
echo("I'm on othersite.com");
If I point my browser to example.com/header.php, I see "I'm on example.com". Similarly, going to othersite.com/header.php, I see "I'm on othersite.com". So far so good.
Here's my problem. The filemydir/b.php
, in both sites, has the linerequire('../header.php')
, so thatheader.php
gets loaded in. Once again, I'm expecting to see the different strings depending on which site I'm visiting. But in my browser, I see "I'm on example.com" when I visit both example.com/mydir/b.php and othersite.com/mydir/b.php.
From reading the documentation for require/include, I'm pretty sure I understand what's happening.
- When I go to othersite.com/header.php, the current working directory is
/home/michael/public_html/
. So therequire('a.php');
line correctly loads/home/michael/public_html/a.php
, because it was able to find it in the current working directory. - When I go to othersite.com/mydir/b.php, the current working directory is
/home/michael/public_html/mydir/
. In this casea.php
is not in the current working directory, so it doesn't get loaded from there. Instead, PHP checks the calling script's directory fora.php
. Since/home/michael/public_html/header.php
is a symbolic link to/var/www/html/header.php
, it always considers the "calling script's directory" to be/var/www/html/
, and so it always loads/var/www/html/a.php
.
That was a lot of detail, but I hope everything is clear.
What I want is some way for/home/michael/public_html/a.php
to be loaded by othersite.com/mydir/b.php. In my head I think I'm looking for some way of obtaining the parent directory ofheader.php
, likedirname(__FILE__)
, but which can also detect when the script was called by a symbolic link, and return the parent directory of the link. Then, I can justchdir(...)
to the directory that I want before callinga.php
, and switch back afterwards.
Of course, I'm very open to other suggestions, but would definitely prefer if they only involved changingheader.php
, and notmydir/b.php
. This is becausemydir/b.php
really represents hundreds of pages at several different directory levels, many of which I do not manage, and it wouldn't be feasible to edit all of them.
Is this possible?
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: sqlstate[23000]: integrity constraint violation: 1452 cannot add or update a child row: a foreign key constraint fails
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.