【问题标题】:PHP : Is it possible to include a file from a sibling directory?PHP:是否可以包含同级目录中的文件?
【发布时间】:2017-08-27 03:30:48
【问题描述】:

您好,在我的主目录中,我有 2 个子目录:subDir_AsubDir_B。在 subDir_A 中有一个名为 file_a.php 的文件。在 subDir_B 我有一个文件 file_b.php。在 file_b.php 我想包含 file_a.php。可行吗?我正在使用 Xampp。

【问题讨论】:

  • include_once(../subDir_A/file_A.php)

标签: javascript php html xampp


【解决方案1】:

这很容易使用 4 个函数之一来完成。

includeinclude_oncerequirerequire_once,请参阅此 http://php.net/manual/en/function.include.php 的文档(我不会对此进行扩展,因为它是非常基本的 PHP 功能)

您应该按照以下方式进行包含。

# you could use any of the other keywords as well.
include __DIR__ . '/../subDir_A/file_A.php'; 

魔术常量__DIR__ 被使用,因为它会考虑相对于它被调用的文件的引用。如果你不这样做而只做include '../subDir_A/file_A.php';,如果你在其他地方包含了file_B.php,它可能不起作用因为它将与调用它的父文件相关。

【讨论】:

  • 好技巧,但 php 仍然很奇怪
【解决方案2】:

是的。

在文件_B.php 中:include('../subDir_A/file_A.php');

../ 表示你想上一个目录。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 2011-05-04
    • 2015-01-31
    相关资源
    最近更新 更多