I am having three files which are as follows
- users.php
- auth.php
- main.php
where users.php is located at root of the directory / . auth and main.php is located into the test folder example /test/auth.php & /test/main.php.
Users.php containing following php code.
<?php
include('test/main.php');
?>
auth.php containing following php code.
<?php
include('../test/main.php');
?>
main.php code
<?php
include('test/db.php');
?>
Where i am executing users.php it is working perfectly but when i am executing users.php which is located at root folder gives me output perfect but if i am executing /test/auth.php
Warning: include(test/db.php) [function.include]: failed to open stream: No such file or directory in /path/db.php on line 2
is there any solution to access auth.php with error with proper function.
Includes are always relative to the working directory (actually, relative to the PATH, of which the working directory is a part). The working directory is determined by the script that’s being executed. Say you have this structure:
When you run
foo.phpeither through$ php foo.phpon the command line or by visitinglocalhost/foo.phpin your browser, the working directory iswebroot/. If you runfolder/bar.php, the working directory iswebroot/folder.To make sure you’re including files relative to the file the
includeis in, use something like:That, or alter your PATH to add your project root to it and always include relative to the project root. I prefer either file relative includes as shown above or autoloading though.