PHP include relative path -
i have file /root/update/test.php. there's file, /root/connect.php; file has line
include "../config.php";
in /root/update/test.php. there's code
set_include_path(".:/root"); include "connect.php";
when run /root/update/test.php, finds connect.php, fails find config.php, giving me
php warning: include(../config.php): failed open stream: no such file or directory in /root/connect.php on line 2 php warning: include(): failed opening '../config.php' inclusion (include_path='.:/root')
this confusing me because warnings make seem i'm doing correctly - include path /root, , it's looking file ../config.php (/config.php), exists. can clear me? note using absolute paths not option me, due deploying production server have no access to.
ubuntu/apache
you include using __dir__
:
include(dirname(__dir__).'/config.php');
__dir__
'magical' , returns directory of current file without trailing slash. it's absolute path, have concatenate file name __dir__
. in case, need ascend directory use php's dirname
ascends file tree, , here can access config.php
.
you set root path in method too:
define('root_path', dirname(__dir__) . '/');
in test.php set root @ /root/
level.
include(root_path.'config.php');
should work include config file want.
Comments
Post a Comment