简体   繁体   中英

How to access config.php file from multi level subdirectories?

I'm having dubts on my website's structure. I've a config, lib and public folders in the root. config contains the config.php and other similiar, lib contains all php/html content (header, each page content, footer etc), while public folder contains all the subfolders for each page with the index.php inside. I did this for the url... Anyway, each of these index.php set their page name in a variable and call the config.php file and a module.php for the whole content. My problem is that since I have multi level subdirectory (news/sport/football) each index.php have different config path to call. The "deepest" files have many "../" before config/config.php.

Here's the index.php inside public/news/:

<?php  
require '../../config/config.php';


$MainPage = 'news';
$CurPage = 'news';


include_once(LIB_PATH. 'modules.php');  
?>

So, is there any way to get the absolute root dir so that the path is the same in all files? Also, is this structure correct?

You should be able to call config.php from anywhere using an absolute path.

include "/config/config.php";

You cannot do this because PHP's ini_path does not include your directory.

Try reading this answer about where/how to add a directory to your php.ini path.

require $_SERVER['DOCUMENT_ROOT'].'/config/config.php';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM