简体   繁体   English

定义config.php的PHP路径

[英]Defining PHP Path to config.php

I have an existing PHP Web project running under XAMPP and have been tidying it up prior to deployment to make it more secure and easier to maintain. 我有一个在XAMPP下运行的现有PHP Web项目,并且在部署之前对其进行了整理,以使其更安全,更易于维护。 One of these steps was to move some key variable into a config.php file similar to PHP Site Templates 这些步骤之一是将一些关键变量移动到类似于PHP网站模板的config.php文件中

I have placed the config.php file within the resources folder as shown here 我将config.php文件放置在资源文件夹中,如下所示

在此处输入图片说明

I now attempt to include the config file as follows 我现在尝试包括以下配置文件

include 'resources/config.php';
echo ("Template path is ".TEMPLATES_PATH );

The result is that the TEMPLATES_PATH definition is not found. 结果是找不到TEMPLATES_PATH定义。 If I now move the config.php file to the root directory (htdocs), that is the same level as the index.php file and change the include to 如果现在将config.php文件移动到根目录(htdocs),则该目录与index.php文件处于同一级别,并将include更改为

include 'config.php';
echo ("Template path is ".TEMPLATES_PATH );

TEMPLATES_PATH is defined and the site works. TEMPLATES_PATH已定义,站点可以正常工作。

I have tried almost every variant of absolute paths to no avail and so this probably points to a fundamental lack of understanding on one or another aspects of PHP path structures that I haven't grasped and so any insight into this would be greatly appreciated. 我几乎尝试了绝对路径的几乎所有变体,但均无济于事,因此,这可能表明对我尚未掌握的PHP路径结构的一个或另一个方面缺乏根本的了解,因此,对此的任何见解将不胜感激。

try 尝试

include __DIR__ . '/resources/config.php';

By the way, using composer is a better choice! 顺便说一句,使用作曲家是更好的选择!

"autoload": {
    "files": ["/resources/config.php"]
}

Well, here is the answer to the mystery. 好吧,这是谜底的答案。 But first thanks for all those that attempted to help. 但是首先感谢所有尝试提供帮助的人。

If I had posted the contents of the config.php file the definition of ` 如果我已经发布了config.php文件的内容,则定义`

define("TEMPLATES_PATH", realpath(dirname(__FILE__) . 'resources/templates'));`

would have been spotted as incorrect. 会被认为是不正确的。 The file path for the config.php file is relative to the file, not the directory structure. config.php文件的文件路径是相对于该文件的,而不是目录结构。 Hence by moving the file to the root directory allowed the above definition to work, but when in the resources folder, the resource directory was over specified. 因此,通过将文件移动到根目录,可以使用上述定义,但是在资源文件夹中时,资源目录已被过度指定。 Hence the solution is to ensure that the specifications within the config.php file are with respect to the location of that file within the project; 因此,解决方案是确保config.php文件中的规范与该文件在项目中的位置有关; hence changing the above to: 因此将以上内容更改为:

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

fixes the problem. 解决问题。

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

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