0

在使用带有纯 php 或 ajax 调用的 php 函数时,我偶然发现了一个(至少对我而言)新的奇怪的路径行为。

如果我只使用 php,则 php 函数中的路径如下:

require_once('wp-content/themes/xxx/tcpdf/tcpdf.php');

但是如果我在 ajax 调用中使用完全相同的函数,则路径需要像这样才能使其工作:

require_once('../tcpdf/tcpdf.php');

你能解释一下为什么会这样吗?非常感谢你!

4

3 回答 3

2

我想当你在没有 AJAX 调用的情况下使用它时,你是从其他文件中包含它,这可能会有所作为。但是,我建议将应用程序根目录存储在一个常量(例如 ROOT)中,然后包含与 ROOT 相关的所有内容。

于 2013-05-16T16:16:32.967 回答
2

您得到这种行为的原因是因为所有包含都发生在 wordpress 安装的根目录中,所以您说在访问文件之前先将目录结构向下执行几层。分解它是这样的。

wp-content/  Go down one directory level from the file i'm in
themes/      Then go down into the themes directory
xxx/         Then go to the xxx directory
tcpdf/       Then go to the tcpdf directory
tcpdf.php  This is the file you want

当您执行第二个包含时,您位于与父共享的目录中,wp-content/themes/xxx/tcpdf/因此您要说的是

../        Go up one directory level
tcpdf/     Go into the tcpdf directory
tcpdf.php  This is the file you want
于 2013-05-16T16:42:59.983 回答
1

将包含路径重新定义为set_include_path ( APP_ROOT ). 通过这样做,所有包含/要求将相对于您将在常量中定义的应用程序根APP_ROOT

PHP 通常将当前目录作为包含路径,这可能会导致您遇到此类问题。

于 2013-05-16T16:22:45.380 回答