【问题标题】:include path is not working when referencing another file引用另一个文件时包含路径不起作用
【发布时间】:2020-01-29 16:24:25
【问题描述】:

我在使用 include()require_once() 时遇到问题,这是我的脚本上驻留在此处的报告发布者文件的内容:

C:\xampp\htdocs\checklists\reports\report-publishers.php

现在我想包含这个文件:

<?php include("images/report-publisher-completed.php");?>

该脚本的目录在这里:

C:\xampp\htdocs\checklists\reports\images\report-publisher-completed.php

问题出在这个report-publisher-completed.php中我有以下内容:

require_once("../../includes/database.php");

这是该文件的位置:C:\xampp\htdocs\checklists\includes\database.php

所以当我运行代码时,我得到以下错误:

( ! ) 警告:require_once(../../includes/database.php): 失败 打开流:中没有这样的文件或目录 C:\xampp\htdocs\checklists\reports\images\report-publisher-completed.php 在第 3 行

( ! ) 致命错误:require_once():需要打开失败 '../../includes/database.php' (include_path='C:\xampp\php\PEAR') 在 C:\xampp\htdocs\checklists\reports\images\report-publisher-completed.php 在第 3 行

我该如何解决这个问题?

谢谢!

【问题讨论】:

    标签: php


    【解决方案1】:

    这样试试

    require_once(dirname(__FILE__).'/../../includes/database.php');
    

    【讨论】:

    • 仍然是一个错误 (!) 警告:require_once(C:\xampp\htdocs\checklists\reports/includes/database.php): 无法打开流:C:\ 中没有这样的文件或目录xampp\htdocs\checklists\reports\images\report-publisher-completed.php 在第 3 行
    【解决方案2】:

    包含文件的更安全的方法是使用当前脚本的相对路径,使用常量__dir__,如下所示:

    include __dir__ . '/includes/database.php';
    

    所以,对于你的情况,可能:

    include __dir__ . '/../../includes/database.php';
    

    【讨论】:

      【解决方案3】:

      试试这个

      require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/database.php";
      

      代替

      require_once("../../includes/database.php");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-14
        • 2018-04-06
        • 2019-07-14
        • 2015-07-12
        • 1970-01-01
        • 2018-10-17
        • 1970-01-01
        • 2021-10-15
        相关资源
        最近更新 更多