简体   繁体   English

如何从多级子目录访问config.php文件?

[英]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. 我在根目录下有一个配置,lib和公用文件夹。 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. config包含config.php和其他类似名称,lib包含所有php / html内容(页眉,每个页面的内容,页脚等),而public文件夹包含每个页面的所有子文件夹,其中包含index.php。 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. 我是为url做的...总之,每个index.php在变量中设置其页面名称,然后调用config.php文件和module.php来获取全部内容。 My problem is that since I have multi level subdirectory (news/sport/football) each index.php have different config path to call. 我的问题是,由于我有多级子目录(新闻/体育/足球),所以每个index.php都有不同的配置路径来调用。 The "deepest" files have many "../" before config/config.php. “最深”文件在config / config.php之前有许多“ ../”。

Here's the index.php inside public/news/: 这是public / news /中的index.php:

<?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. 您应该可以使用绝对路径从任何地方调用config.php。

include "/config/config.php";

You cannot do this because PHP's ini_path does not include your directory. 您无法执行此操作,因为PHP的ini_path不包含您的目录。

Try reading this answer about where/how to add a directory to your php.ini path. 尝试阅读有关在何处/如何将目录添加到php.ini路径的答案

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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