PHP Require In different folders

廉价感情. 提交于 2019-12-11 02:57:02

问题


Okay so on my website I have a scripts folder which includes a php file which connects to mysql server so If I move a database then It will change it on all the files which are connect to the database.

I also have another folder called templates. In that folder there is a top for the header and the footer. In the header template I wrote:

require("../scripts/connect.php");

And I have another folder called, category. And that folder includes the header and the header includes connect. But then it displays and error that there is no such files.

Please help. Thank you


回答1:


A good practice is to include a main config in all running php files, usually called config.php :)

in this config file create a constant called SITE_ROOT or something similar that point to the exact folder like this define("SITE_ROOT", "/var/www/mysite");

Then on any include, include_once, require, require_once use it like this:

require(SITE_ROOT."/scripts/connect.php");

This should solve any relative path drama




回答2:


You shouldn't use relative paths with the include/require, but use a constant defining the ROOT_PATH of your website.

Example: In all the files calling needing includes:

define(ROOT_PATH, '../');

include ROOT_PATH . '/scripts/connect.php';

And in /scripts/connect.php (and all the other files that will be included somewhere), all the includes should use ROOT_PATH (without defining it).



来源:https://stackoverflow.com/questions/12297096/php-require-in-different-folders

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!