【问题标题】:PHP access same file from different foldersPHP从不同文件夹访问同一个文件
【发布时间】:2015-01-06 13:25:03
【问题描述】:

今天我发现从我的网站的某些部分访问我的语言文件有问题。我的结构在这里:

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

在 globals.php 我包括

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

当从 index.php 调用 globals.php - include_once 'php/globals.php'; 时,一切正常,但我有 readDB.php,writeDB.php,它调用 include_once 'globals.php';并且由 AJAX 从 index.php 调用,在这种情况下,它包括不存在的 php/languages/lang.xx.php。您有什么想法吗,如何以正确的方式写入 lang.xx.php 的路径,以便从根目录和任何其他文件夹中使用它?

我正在考虑类似的事情

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

但是,这样对吗?

【问题讨论】:

标签: php


【解决方案1】:

答案:

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

现在它可以在站点树中的任何位置工作。

【讨论】:

    猜你喜欢
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    相关资源
    最近更新 更多