PHP要求在不同的文件夹中 [英] PHP Require In different folders

查看:92
本文介绍了PHP要求在不同的文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,在我的网站上,我有一个scripts文件夹,其中包含一个连接到mysql服务器的php文件,因此,如果我移动数据库,它将在连接到数据库的所有文件上对其进行更改.

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");

我还有另一个名为category的文件夹.该文件夹包含标题,而标题包含connect.但是随后显示并出现错误,提示没有此类文件.

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.

请帮助.谢谢

推荐答案

一个好的做法是在所有正在运行的php文件中包含一个主要配置,通常称为config.php:)

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

在此配置文件中创建一个名为SITE_ROOT的常量或类似这样的常量,它指向确切的文件夹 define("SITE_ROOT", "/var/www/mysite");

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");

然后在任何include,include_once,require,require_once上使用它,如下所示:

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

这篇关于PHP要求在不同的文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆