anon
anon

Reputation:

Using absolute paths in PHP not working

I am trying to use absolute paths to get a definitive reference to a config file.
For this, I am currently doing this:

require_once '../config.php'; //Works fine

But when I try this:

require_once '/config.php'; //Throws error, see below

The error I get is:

require_once(): Failed opening required '/config.php' (include_path='.;C:\php\pear')

Is this a thing in PHP 5.6.0 or am I missing something?

Upvotes: 0

Views: 147

Answers (2)

Professor Abronsius
Professor Abronsius

Reputation: 33823

If you explicitly set the includes path in your script to the actual location it should be quite simple to include the files you need such as:

set_include_path( $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR );
require_once( 'config.php' );

Upvotes: 0

Ben
Ben

Reputation: 9001

Breaking down ../config.php:

  1. ../ goes one directory above the working directory
  2. config.php finds the file config.php in that directory

Breaking down /config.php:

  1. / goes to the server root
  2. config.php finds the file config.php in that directory

If the first one works but not the second, it means that the file config.php is not in the root folder, but is in the parent directory of the working file.

Upvotes: 2

Related Questions