SilentCry
SilentCry

Reputation: 2092

PHP access same file from different folders

today I discover problem with accessing my language file from parts of my website. My structure is here:

ROOT
|___ index.php
|___ langugages
     |___ lang.cs.php
     |___ lang.en.php
|___ php
     |___ globals.php
     |___ readDB.php
     |___ writeDB.php
     ...

In globals.php I'm including

include './languages/lang.' . $lang . '.php';

When globals.php is called from index.php - include_once 'php/globals.php';, everything is OK, but I have readDB.php, writeDB.php, which calls include_once 'globals.php'; and which are called by AJAX from index.php and in this case it including php/languages/lang.xx.php, which not exists. Do you have any idea, how to write path to lang.xx.php in right way for using it from root and from any other folder too?

I'm thinking about something like

if (./languages/lang.xx.php not exists) include ../languages/lang.xx.php;

But, is it right way?

Upvotes: 0

Views: 73

Answers (1)

SilentCry
SilentCry

Reputation: 2092

Answer:

set_include_path(implode(PATH_SEPARATOR, array(__DIR__, get_include_path())));
include_once __DIR__ . '/../languages/lang.' . $lang . '.php';

Now it works from any position in site tree.

Upvotes: 1

Related Questions