1

I have set apache so that all requests go to a file /var/www/common_index.php

Now, common_index.php looks at requested file name and sources appropriate file /var/www/123/public_html/requested_file.php

I am having problems when I include a file (with relative path) in requested_file.php. It tries to search for file in /var/www instead of /var/www/123/public_html/

How to solve this?

3 Answers 3

6

You can change the working directory in requested_file.php before calling include to make it work:

chdir(dirname(__FILE__));
include 'path/to/file.php';

or for PHP 5.3+

chdir(__DIR__);
include 'path/to/file.php';

If you don't want to change the working directory (which will affect other file system operations) then just append the path each time you do an include using the magic constant __DIR__:

include dirname(__FILE__) . '/path/to/file.php';
include __DIR__ . '/path/to/file.php'; # for PHP 5.3+

where the path is relative to the file where you used the code above.

Sign up to request clarification or add additional context in comments.

Comments

0

Edit your DocumentRoot in your Apache config.

Set it to /var/www/123/

1 Comment

Then how do I ensure that all requests go to /var/www/common_index.php ? (I need it for some reason). Also... the public_html path varies....
0

You can use __DIR__:

$path = __DIR__;

Whereas $path contains the path of the file which contains this line

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.