警告:include(../config/config.php) [function.include]:无法在 PHP 中打开流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8634543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: include(../config/config.php) [function.include]: failed to open stream in PHP
提问by Manoj
i'm getting following error in my PHP file.
我的 PHP 文件出现以下错误。
Warning: include(../config/config.php) [function.include]: failed to open stream: No
such file or directory in C:\xampp\htdocs\my-proj\functions\function.php on line 2
Warning: include(../config/config.php) [function.include]: failed to open stream: No
such file or directory in C:\xampp\htdocs\my-proj\functions\function.php on line 2
let me describe my folder structure
让我描述一下我的文件夹结构
- ROOT folder /index.php
- functions / function.php
- config / config.php
- signup / signup.php
- ROOT 文件夹 /index.php
- 函数/function.php
- 配置/config.php
- 注册/注册.php
now, If i use absolute path, then it is give the same error in signup.php, and if I use relative path then it is giving this error in index.php
现在,如果我使用绝对路径,那么它会在 signup.php 中给出相同的错误,如果我使用相对路径,那么它会在 index.php 中给出这个错误
any help would be appreciated.
任何帮助,将不胜感激。
回答by mack
use
用
include("$_SERVER[DOCUMENT_ROOT]/config/config.php");
回答by mario
The file paths are relative to the invokedscript. If your application gets invoked by http requests to index.php
, then the include()
path needs to be relative to that - even if the include statement itself is located in the functions.php script.
文件路径相对于调用的脚本。如果您的应用程序被 http 请求调用到index.php
,则include()
路径需要与该路径相关 - 即使 include 语句本身位于 functions.php 脚本中。
A common workaround is to make all paths absolute in relation to the document root:
一个常见的解决方法是使所有路径都相对于文档根为绝对路径:
include("$_SERVER[DOCUMENT_ROOT]/config/config.php");
// Note: leaving out array keys only valid in double quote string context.
That would work in index.php and functions.php alike.
这将在 index.php 和 functions.php 中工作。
回答by Chinoto Vokro
Use include __DIR__."/../config/config.php";
if you want to include a file relative to the file you're currently executing in. If you're using a version of php older than 5.3.0 (you shouldn't), replace __DIR__
with dirname(__FILE__)
.
使用include __DIR__."/../config/config.php";
,如果你想包括相对于您目前所执行的文件的文件。如果你使用一个版本的PHP 5.3.0比年长的(你应该),替换__DIR__
用dirname(__FILE__)
。
$_SERVER['DOCUMENT_ROOT']
is not set when using commandline and requires that your project is relative to the DOCUMENT_ROOT instead of allowing the user to place it wherever they please. If phpMyAdmin used this variable, you would be forced to accommodate it instead of just placing it wherever you want. That's another thing, it's a variable, so there's a potential security issue too.
$_SERVER['DOCUMENT_ROOT']
使用命令行时未设置,并要求您的项目与 DOCUMENT_ROOT 相关,而不是允许用户将其放置在任何他们喜欢的地方。如果 phpMyAdmin 使用了这个变量,你将被迫适应它,而不是把它放在你想要的任何地方。那是另一回事,它是一个变量,因此也存在潜在的安全问题。
If config.php is necessary, I suggest using require
instead, otherwise use if (file_exists($file)) {require $file;}
so you can avoid warnings when it doesn't exist and get an error when it can't be read (I assume if it exists, it's intended to be used).
如果 config.php 是必要的,我建议require
改用,否则使用if (file_exists($file)) {require $file;}
它可以避免在它不存在时发出警告并在无法读取时得到错误(我假设它存在,它打算使用)。