Alberto
Alberto

Reputation: 12899

Get included file real path

I have a project with the following structure:

.credentials
helpers.php
home.php
admin\
   login.php
   register.php

In the helpers.php, i'm trying to get the content of .credentials by doing file_get_contents('.credentials'), but this works only if the then i include that file in file in the same directory (for example home.php), but when I try to do something like:

include('../helpers.php');

in the admin/login.php file, it no longer works, because helpers.php tries to find admin/.credentials

How can i avoid this behavior

Upvotes: 1

Views: 59

Answers (1)

Honk der Hase
Honk der Hase

Reputation: 2488

Always use absolute pathes for includes.

Use the constant __DIR__ to refer to the (real!) directory of the current PHP file.

Example (from home.php):

include __DIR__ . '/admin/login.php';

Upvotes: 2

Related Questions