【问题标题】:Fatal error: require_once() (include_path='.;C:\php5\pear') [duplicate]致命错误:require_once() (include_path='.;C:\php5\pear') [重复]
【发布时间】:2012-06-26 15:26:31
【问题描述】:

我正在做一个 PHP 项目,我在 require_once(甚至在 require)中遇到错误

Warning: require_once(C:/wamp/www/MyFiles/MyProject/includes/db_config.php) [function.require-once]: failed to open stream: No such file or directory in C:\wamp\www\MyFiles\MyProject\includes\autoloads.php on line 9

Fatal error: require_once() [function.require]: Failed opening required 'C:/wamp/www/MyFiles/MyProject/includes/db_config.php' (include_path='.;C:\php5\pear') in C:\wamp\www\MyFiles\MyProject\includes\autoloads.php on line 9

这就是我包含文件的方式

$root = $_SERVER['DOCUMENT_ROOT'];
//config..
require_once($root . '/MyFiles/MyProject/includes/db_config.php');

我尝试过使用

echo $root . '/MyFiles/MyProject/includes/db_config.php';

正在正确打印 URL

C:\wamp\www\MyFiles\MyProject\includes\db_config.php

我正在使用 WAMP 服务器 5 autload.php 和 db_config.php 在同一个文件夹中

【问题讨论】:

  • 确定文件 (db_config.php) 位于该目的地吗?
  • 是的,文件位于同一路径
  • 我必须在 PHP.ini 中进行任何配置吗?
  • 密码从不说谎。不可能。
  • 好吧,如果它真的在打印C:\wamp\www\MyFiles\MyProject\includes\db_config.php,那么没有。

标签: php


【解决方案1】:

仔细检查文件是否存在于服务器上。如果你不能因为任何原因,这会做到:

if(file_exists($myFile)){
echo"The file is there";
}//end of file exists
else{
echo"the file does NOT exist....fool";
}//end of else - the file is not there

这是我能想到的唯一问题 - 文件不在它应该在的位置...试试上面的方法并让我更新。

【讨论】:

    【解决方案2】:

    Linux 和 Windows 机器上的目录分隔符不同也可能是个问题。 也许尝试使用常量 DIRECTORY_SEPARATOR 而不是硬编码的分隔符?

    【讨论】:

    • 顺便说一句。如果您在代码中说 echo $root . '/MyFiles/MyProject/includes/db_config.php'; 并回显 C:\wamp\www\MyFiles\MyProject\includes\db_config.php 我认为您的斜杠和反斜杠可能有问题
    【解决方案3】:

    检查文件是否存在,如果存在,检查文件的权限。

    【讨论】:

      【解决方案4】:
      1. 试试这个需要构造

        require 'file';

      2. ..或此根设置

        $root = realpath($_SERVER['DOCUMENT_ROOT']);


      更新 使用虚拟主机


      【讨论】:

      • @SyedSalmanRazaZaidi 您是否尝试删除路径中的空格?
      【解决方案5】:

      文件可读吗?

      试试这个:

      <?php
      echo "Does file exist?". file_exists($file) ? 'true' : 'false';
      echo "Is it readable?".is_readable($file) ? 'true' : 'false';
      require_once $file;
      

      您还可以使用相对于您所在文件的路径,而不是依赖 DOCUMENT_ROOT 字符串:

      <?php
      $root = realpath(dirname(__FILE__));
      

      此外,windows 中单独的目录默认是 \,而不是 /(尽管 / 也应该解析,即使对于 require)。您可以使用一个预定义的常量,以确保它在任何地方都兼容:

      require_once($root . DIRECTORY_SEPARATOR . 'MyFiles' . DIRECTORY_SEPARATOR . 'MyProject' . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'db_config.php';
      

      此外,如果您正在使用 APC 缓存,如果文件的缓存过时,您可以尝试重新启动它。 (E.G filemtime 不正确)

      【讨论】:

      • 感谢 Jon 的回复,这两个语句都打印为 true,但 reuquire_once 的此代码给出了相同的错误:(
      猜你喜欢
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      • 2015-04-08
      • 2021-03-19
      • 2020-02-14
      • 2014-03-05
      • 2014-10-02
      • 2021-03-22
      相关资源
      最近更新 更多