简体   繁体   English

PHP从不同的文件夹访问同一文件

[英]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 在globals.php中,我包括

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'; 当从index.php-include_once'php / globals.php';调用globals.php时,一切正常,但是我有readDB.php,writeDB.php,它们调用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. 并且由AJAX从index.php调用,在这种情况下,它包括不存在的php / languages / lang.xx.php。 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? 您有什么主意,如何以正确的方式将lang.xx.php的路径写入以从根目录以及其他任何文件夹中使用它?

I'm thinking about something like 我在想类似的东西

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

But, is it right way? 但是,这是正确的方法吗?

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. 现在,它可以在站点树中的任何位置工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM