PHP:包含和要求文件时抛出错误 [英] PHP: Error thrown when including and requiring files

查看:208
本文介绍了PHP:包含和要求文件时抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建一个引导程序文件,但每当我尝试在另一个文件中包含或要求它时,会出现这样的错误:



警告: require_once(../ folder / file.php)[function.require-once]:无法打开流:没有这样的文件或目录......



致命错误: require_once()[function.require]:在...中打开所需的'../folder/file.php'(include_path =':')失败>

绘制整个场景:
我有一个bootstrap文件,load.php。在其中,我连接到../config/config.php的配置文件。在加载文件上方,我在classes / database.php中有一个数据库类文件。在数据库文件中,我写了这个if语句:

  if(file_exists('../ load.php')){ 
echo'load.php:它存在';
require_once('../ load.php');
} else {
echo'load.php:它不存在';
}

在加载文件中我写了一个类似的if语句,但检查是否配置文件存在。令人惊讶的是,当我加载database.php文件时,我得到:

  load.php:它存在
config.php :文件不存在

但是当我加载load.php文件时,我得到:

  config.php:存在

以下是我的文件所在位置:



根目录: / Library / WebServer / Documents / project



config.php文件 /Library/WebServer/Documents/project/config/config.php



load.php文件: /Library/WebServer/Documents/project/includes/load.php



database.php file: /Library/WebServer/Documents/project/includes/classes/database.php



我需要这个bug的帮助。
谢谢,

解决方案

使用 dirname(__ FILE__)。 ../相对/路径/从/文件/中/哪个/打开/到/ config.php中 。从PHP 5.3开始,还有 __ DIR __ 常量


I tried to create a bootstrap file, but whenever I try to include or require it in another file an error like this keeps showing up:

Warning: require_once(../folder/file.php) [function.require-once]: failed to open stream: No such file or directory in...

Fatal error: require_once() [function.require]: Failed opening required '../folder/file.php' (include_path='.:') in...

To paint the entire scenario: I have a bootstrap file, load.php. In it, I connect to the config file at ../config/config.php. Above the load file I have a database class file at classes/database.php. In the database file I wrote this if-statement:

           if ( file_exists('../load.php') ) {
               echo 'load.php: It exists';
               require_once('../load.php');
           } else {
                  echo 'load.php: It does not exist';
                  }

In the load file I wrote a similar if-statement, but checking if the config file exists. Surprisingly, when I load the database.php file I get:

load.php: It exists
config.php: file doesn't exist

But when I load the load.php file and I get:

config.php: It exists

Here is where my files are located:

Root Directory: /Library/WebServer/Documents/project

config.php file /Library/WebServer/Documents/project/config/config.php

load.php file: /Library/WebServer/Documents/project/includes/load.php

database.php file: /Library/WebServer/Documents/project/includes/classes/database.php

I need help with this bug. Thanks,

解决方案

use dirname(__FILE__) . '../relative/path/from/file/in/which/opened/to/config.php'. From PHP 5.3 there is also __DIR__ constant

这篇关于PHP:包含和要求文件时抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆