silkfire
silkfire

Reputation: 25945

Including a file in an included folder's parent directory fails

I have included a folder that contains some classes, in my .htaccess file.

Somewhere deeper under public_html I'm including a PHP file from this folder. Works fine, the include_path is in effect.

But if I write something like this:

require_once '../DB.php';

it fails?

This file is just above the included folder, why can't the PHP engine find it?

Upvotes: 1

Views: 137

Answers (1)

complex857
complex857

Reputation: 20753

The relative paths are always interpreted from the current working directory (usually referred to as CWD). With web requests this is usually the file's directory that got the request to handle, but can be changed any time with chdir(), you can check it's current value with getcwd().

If you want your code to be resilient against the cwd, you can use the __DIR__ magic constant or if you are on an ancient php version (before 5.3) the dirname(__FILE__) in your includes, like this:

require_once __DIR__.'/../DB.php';

Upvotes: 2

Related Questions