【发布时间】:2016-06-24 20:52:48
【问题描述】:
我“需要”一个有自己的 require() 调用的文件。但是调用 set_include_path() 不适用于 require() 中的相对路径。不幸的是,我无法控制所需的代码文件。
C:/myapp/index.php - 我的代码
set_include_path(get_include_path() . ';C:/app/subfolder');
require('C:/app/subfolder/index.php');
C:/app/subfolder/index.php - 第三方代码(无控制)
require('../config.php'); // Require file located at C:/app/config.php
结果:
打开失败需要'../config.php' (include_path='.;C:/app/subfolder')
这不起作用,即使 C:/app/subfolder 在包含路径中。我希望 ../config.php 是 C:/app/subfolder/../config.php。可能我做得不对。
在这种情况下,C:/app/subfolder/index.php 原本从未打算包含在另一个 PHP 文件中。但是,我正在它前面构建代码,并且我希望 C:/app/subfolder/index.php 仍然能够要求/包含文件,就好像它从未离开过它的位置一样。
我已经读到使用 dirname() 或 __DIR__ 修复了 require() 中的相对路径问题,但我无法更改 C:/app/subfolder/index.php 中的代码。 p>
有没有办法让它工作?
【问题讨论】:
-
set_include_path()或get_include_path()不能这样工作。您可以改用相对路径require_once('subfolder/index.php');。 -
你试过了吗:
set_include_path('C:/app/subfolder');? -
您确定
config.php存在于C:/app目录中吗? include_path 检查相对路径,所以如果文件不在C:/app中,它肯定会失败。
标签: php