Is there a simpler way to find the "AboveTheRoot" path?

I am in the process of writing a demo which shows source files and requires database access.

I managed to solve the problem of not publishing my database login details but convince there must be a better way.

 // Database Access Details
    $docRoot    = $_SERVER['DOCUMENT_ROOT'];
    $lastPath   = strrchr($docRoot, '/');
    $aboveRoot  = str_replace($lastPath, '/', $docRoot);
    $aboveRoot .= '_dbPARAMS.php';
    if( file_exists($aboveRoot) ):
      require $aboveRoot;
    else:
      define('HOST',  'yourhost');
      define('uName', 'userName');
      define('pWORD', 'passWord');  
      define('dBase', 'dataBase'); 
    endif;

$conn =  mysqli_connect(HOST, uNAME, pWORD, dBASE);
//

You could try this.

  $aboveRoot = dirname(__DIR__);
  $dbConf = $aboveRoot."/_dbParams.php";

Scott

dirname(DIR); returns the parent of the current directory.

I would like the secure path above the document root:

// current working directory
echo getcwd();  //  /home/john/www/test/current-working-path

// parent directory of current directory
echo dirname(__DIR__); // /home/john/www/test

// document root
echo $_SERVER["DOCUMENT_ROOT"];  // /home/john/www

// secure path required  above the document root
//  /home/john/

// confidential information not available to public
$dbConf = "/home/john/_dbParams.php";

Ah. Ok. Then try:

 $aboveRoot = dirname($_SERVER["DOCUMENT_ROOT"]);
 $dbConf = $aboveRoot."/_dbParams.php";

Scott

1 Like

Surely, that’s just root, not above root?

Such tricks should work too:

$dbConf = $aboveRoot."/../_dbParams.php";

Thank you very much, that works fine. Far more leaner and meaner :smile:

Perhaps dirname(…); should be renamed parentpath(…).

I have used that and found it was trial and error to gradually add …/ until I hit upon the correct path. It also fails if the path is moved to a different level whereas this works every time:

// Secure Database Access Details 
    $aboveRoot  = dirname($_SERVER["DOCUMENT_ROOT"]); # NO TRAILING SLASH
    $aboveRoot .= '/_dbPARAMS.php';
    if( file_exists($aboveRoot) ):
      require $aboveRoot;

    else:
      define('HOST',  'yourhost');
      define('uName', 'userName');
      define('pWORD', 'passWord');  
      define('dBase', 'dataBase'); 
    endif;

Ah - I see! :slight_smile:

1 Like

lol Perhaps indeed!

Description
string dirname ( string $path [, int $levels = 1 ] )

Given a string containing the path of a file or directory, this function will return the parent directory’s path that is levels up from the current directory.

* “levels” is version 7

7.0.0 Added the optional levels parameter.

1 Like

Yeah, one of the things that hurts PHP as a language in my opinion is the naming of functions. I am not sure why some function names are what they are.

Scott

1 Like

The php functions tend to follow the naming of their c equivalents. Specifically the string functions.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.