user1572008
user1572008

Reputation:

PHP Require In different folders

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

Upvotes: 3

Views: 13128

Answers (2)

Hawili
Hawili

Reputation: 1659

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

Upvotes: 12

Bgi
Bgi

Reputation: 2494

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

Upvotes: 2

Related Questions