【问题标题】:require_once with subfolders [closed]带有子文件夹的require_once [关闭]
【发布时间】:2013-02-09 13:42:07
【问题描述】:

我的文件夹主页有两个子文件夹,如下所示

+Home  
 +include  
   - membersite_config.php

+iDiscover  
   -index.php

在 index.php 中我添加了 require_once 脚本来访问 membersite_config.php

<?PHP

require_once($_SERVER['DOCUMENT_ROOT'].'/include/membersite_config.php');
?>

我收到以下错误: 警告:require_once(./include/fg_membersite.php):无法打开流:第 2 行的 D:\Home\include\membersite_config.php 中没有这样的文件或目录
致命错误:require_once():在第 2 行的 D:\Home\include\membersite_config.php 中打开所需的 './include/fg_membersite.php' (include_path='.;C:\php\pear') 失败

错误提示 No such file or directory 而路径“D:\Home\include\membersite_config.php”是正确的。当我将 index.php 移动到 root 下时,页面运行良好。

我也尝试使用here 描述的以下代码,但它给出了相同的错误

<?PHP


define('ROOT', 'D:Home\\');
require_once(ROOT ."/include/membersite_config.php");
?>

编辑:

我也试过 require_once("../include/membersite_config.php"); 它给出了同样的错误

提前致谢

【问题讨论】:

  • 你检查过文件权限了吗?
  • 试试 require_once("../include/membersite_config.php");
  • @Ares 是的,当我将 index.php 移动到根目录时,一切正常。

标签: php require-once


【解决方案1】:

试试这个:

require_once("../include/membersite_config.php");

别忘了检查文件/文件夹权限!

【讨论】:

  • 谢谢。我试过了,但它给出了同样的错误。文件夹权限是完全控制的。当我将 index.php 移动到根目录而不是将其放在 iDiscover 文件夹中时,我可以访问该文件
【解决方案2】:

试试这个:

<?PHP
require_once('../include/membersite_config.php');
?>

【讨论】:

  • 谢谢。但它给出了同样的错误
  • 你把代码放在这里了吗?:iDiscover/index.php
  • 是的。 iDiscover 是根 (Home) 文件夹下的子文件夹
  • 请确认路径:home/iDiscover/index.php or iDiscover/index.php
  • 物理路径是home/iDiscover/index.php,membersite_config路径是home/include/membersite_config.php
【解决方案3】:

'..' 表示返回

require_once('../include/membersite_config.php');

表示取出你所在的文件夹,然后输入include...

如果您必须取出三个文件夹,请执行此操作 ../../../include... 等。

【讨论】:

    【解决方案4】:

    错误意味着您的 membersite_config.php 中有一个在第 2 行不起作用的要求。

    【讨论】:

    • 我可以看到它不起作用。它说 failed to open stream: No such file or directory ,而目录确实存在。这就是为什么我要问我的代码有什么问题
    • 阅读我输入的内容。错误不在 index.PHP 中。
    • index.php 无法访问 membersite_config.php ,当它位于子文件夹 iDiscover 中时。当我将 index.php 移动到根目录时。相同的代码可以正常工作。
    【解决方案5】:
    require_once(dirname(dirname(__FILE__)).'/home/include/membersite_config.php');
    

    【讨论】:

      【解决方案6】:

      你必须像这样包含

      require_once(dirname(__FILE__) . '/include/membersite_config.php');
      

      因为在httpd.conf中不能设置DocumentRoot

      【讨论】:

        【解决方案7】:

        调试试试这个:

        <?php
        $dir_files=glob($_SERVER['DOCUMENT_ROOT'].'/include/*');
        print_r($dir_files);
        ?>
        

        运行此脚本后,您将查看文件名是否正确。

        【讨论】:

        • 我试过了,文件确实存在
        • 我想它的拼写与 membersite_config.php 完全一样?
        • 如果你运行membersite_config.php,你会得到什么错误? (就像 Guido Jansen 建议的那样)
        • membersite_config.php 有效。当我将 indexx.php 移动到根文件夹而不是 iDiscover 文件夹时,相同的代码运行良好
        • 你试过 include() 了吗?
        猜你喜欢
        • 2017-02-23
        • 2013-01-27
        • 1970-01-01
        • 1970-01-01
        • 2019-08-12
        • 2016-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多