I'm working with an existing site, where the files are set up like this:
(index.php)
include_once("functions/Functions.php");
...
(Functions.php)
function GetAValue()
{
include_once("settings/settings.php");
}
So the index file calls functions in a function folder, and the sql variables and whatnot are stored in a settings folder like so:
(folder) functions
(folder) settings
(file) index.php
My question is this: I want to add a new working directory, mobile. If I use include_once("../functions/functions.php");
, it will error when the function tries to include the settings file, as it is looking for "mobile/settings/settings.php" which doesn't exist.
What are my options here ?
I always use a config file that's stored in the public root. In the config I define a Constant
for the base root and one for public root.
<?php
// myconfig.php -- stored in the public root aka document root
$direx = explode("/", getcwd());
DEFINE ('BASEROOT', '/'.$direx[1].'/'.$direx[2].'/'); // host/mysite/
DEFINE ('PUBLICROOT', '/'.$direx[1].'/'.$direx[2].'/'.$direx[3].'/'); // host/mysite/public_html/
?>
require
the config at the top of all pages.
<?php
require_once('myconfig.php'); // called on every page
//example uses
IF (File_Exists(PUBLICROOT.'some_folder/file.txt')) { /* do something */ }
include_once(PUBLICROOT.'some_folder/file.txt');
?>
To include the config in a document root / subdir scenario you can use the following.
Require(str_replace("subdir", "", getcwd()).'myconfig.php');