简体   繁体   中英

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. One of these steps was to move some key variable into a config.php file similar to PHP Site Templates

I have placed the config.php file within the resources folder as shown here

在此处输入图片说明

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. 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

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

TEMPLATES_PATH is defined and the site works.

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.

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 `

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. 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; hence changing the above to:

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

fixes the problem.

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