-1

我正在开发一个 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

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

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

4

5 回答 5

0
  1. 试试这个需要构造

    require 'file';

  2. ..或此根设置

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


更新 使用虚拟主机


于 2012-06-25T11:18:02.700 回答
0

文件可读吗?

尝试这个:

<?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 中单独的目录是 \,而不是 /(alltough / 也应该解析,即使对于 require 也是如此)。您可以使用一个预定义的常量,以确保它在任何地方都兼容:

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

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

于 2012-06-25T11:18:45.130 回答
0

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

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

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

于 2012-06-25T11:19:40.177 回答
0

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

于 2012-06-25T11:20:38.503 回答
0

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

于 2012-06-25T11:21:42.743 回答