1

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 ?

1
  • 1
    Use __DIR__. Always. Commented Aug 22, 2016 at 19:14

1 Answer 1

1

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');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.