php - Obtaining the current working directory of a symbolic linkGet the solution ↓↓↓
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 line
require('a.php');. What I'm expecting is, if I'm visiting example.com, this
require will load in the file at
/var/www/html/a.php, but if I'm on othersite.com, it instead will load
For extra clarity, let's say those two
a.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");
Here's my problem. The file
mydir/b.php, in both sites, has the line
require('../header.php'), so that
header.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 the
require('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 case
a.phpis not in the current working directory, so it doesn't get loaded from there. Instead, PHP checks the calling script's directory for
/home/michael/public_html/header.phpis 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
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 of
dirname(__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 just
chdir(...) to the directory that I want before calling
a.php, and switch back afterwards.
Of course, I'm very open to other suggestions, but would definitely prefer if they only involved changing
header.php, and not
mydir/b.php. This is because
mydir/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?
People are also looking for solutions of the problem: can't write image data to path
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.
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.