【问题标题】:php include_once pathphp include_once 路径
【发布时间】:2012-02-23 14:07:12
【问题描述】:

我正在尝试找到一种方法使 include_once(); 路径从开始目录开始,然后找到路径,例如而不是../../../path/to/file,我将拥有/path/to/file。如果我这样做 /path/to/file 它说没有这样的文件或目录,这是我的直接代码

<?php
include_once("/assets/page_assets.php");
?>

【问题讨论】:

  • path start from the beginning directory 来自网络服务器的根目录? /path/to/file 是文件系统根目录的路径!

标签: php file path include


【解决方案1】:

如果你有 Apache 服务器

<?php
include_once($_SERVER['DOCUMENT_ROOT'] . "/assets/page_assets.php");
?>

"/assets/page_assets.php" 表示from the root of the disk,而不是来自服务器的根文件夹。如果您在谈论其他一些beginning directory,则将其定义为单独的常量/变量的物理路径(服务器文件系统中的路径,而不是 Web 路径),并将其添加到包含的路径中,如上所示。

【讨论】:

    【解决方案2】:

    您可以明确指定包含路径:

    <?php ini_set('include_path',ini_get('include_path').':../../../:');  ?>
    

    但正如 Cheery 在评论中提到的,您必须包含您的文件而不带前导斜杠:

    <?php
    include_once("assets/page_assets.php");
    ?>
    

    【讨论】:

      【解决方案3】:

      为此,您必须使用从根目录开始的路径。在某些情况下,这可能看起来像 /var/www/mysite.com/assets/page_assets.php。找到该路径的一种方法是使用__FILE__(前后各有2个下划线)。如果您从文件中回显它,它将显示完整的路径。您应该能够使用它来设置正确的完整路径。

      【讨论】:

        【解决方案4】:

        chdir($_SERVER['DOCUMENT_ROOT']); 开始您的脚本

        现在你可以调用include("path/to/file.php");,它会从webroot开始查找。

        【讨论】:

          猜你喜欢
          • 2011-12-11
          • 2012-12-24
          • 1970-01-01
          • 2011-08-06
          • 1970-01-01
          • 1970-01-01
          • 2018-12-02
          • 2012-07-08
          • 2016-04-28
          相关资源
          最近更新 更多