Search code examples
phprelative-pathinclude-path

php require relative path


When I require any PHP file in my index page I see an error:

my config file :

define( "APP_ROOT", realpath( dirname( __FILE__ ) ).'/' );

my includes/require file put :

localhost/test/includes/

failing code and error :

require_once(APP_ROOT .'includes/test1.php');
require_once(APP_ROOT .'includes/test2.php');

failed to open stream: No such file or directory in C:\xampp\htdocs\test\includes\test.php on line 38

What is my problem? How can I fix this for any relative path.


Solution

  • It looks like your file is being called from /test/includes, so APP_PATH is C:\xampp\htdocs\test\includes\, so your trying to require C:\xampp\htdocs\test\includes\includes/test1.php and so forth.

    Echo APP_PATH and see what it prints.

    Your better off making all the includes relative to each other, and then you only have to play games importing a single common.php file from your index.php