PHP 致命错误:需要打开失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5437453/
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
PHP Fatal Error: failed opening required
提问by JM4
I have a script called 'sess-start.php' which lies in an /include directory within my httpdocs directory.
我有一个名为“sess-start.php”的脚本,它位于我的 httpdocs 目录中的 /include 目录中。
My site continues to give this error:\
我的网站继续出现此错误:\
[Fri Mar 25 14:52:24 2011] [error] [client 12.3.3.3] PHP Fatal error: require() [function.require]: Failed opening required '/includes/sess-start.php' (include_path='.:') in /var/www/vhosts/site.com/httpdocs/page.php on line 4, referer: https://www.site.com/
[2011 年 3 月 25 日星期五 14:52:24] [错误] [客户端 12.3.3.3] PHP 致命错误:require() [function.require]:无法打开所需的“/includes/sess-start.php”(include_path=' .:') 在第 4 行的 /var/www/vhosts/site.com/httpdocs/page.php,引用:https: //www.site.com/
even though www.site.com/includes DOES exist. What gives!
即使 www.site.com/includes 确实存在。是什么赋予了!
Edit 1
编辑 1
These are includes/requires which may themselves contain additional require or include statements. Relative paths WILL NOT WORK so please do not suggest such.
这些是includes/requires,它们本身可能包含额外的require 或include 语句。相对路径将不起作用,所以请不要提出这样的建议。
My .htaccess file already points all include paths to the site root directory:
我的 .htaccess 文件已经指向站点根目录的所有包含路径:
php_value include_path .:/var/www/vhosts/site.com/httpdocs
php_value include_path .:/var/www/vhosts/site.com/httpdocs
Edit 2
编辑 2
Not all of my include or required scripts are contained with a single directory so suggesting to simply place the /includes directory in the include_path is negated as well (and in turn also causes problems on a windows machine)
并非我所有的包含或必需的脚本都包含在单个目录中,因此建议简单地将 /includes 目录放在 include_path 中也是无效的(反过来也会导致 Windows 机器出现问题)
Update
更新
Perhaps some clarification on a real-life example may help to resolve the issue our team is trying to solve for:
也许对现实生活中的例子进行一些澄清可能有助于解决我们团队试图解决的问题:
On one page, a user may enter a number of options, the following page makes its necessary calculations and based on that will route the customer in a number of potential options, all which lead to require statements for something like a DB entry.
在一个页面上,用户可以输入多个选项,下一个页面进行必要的计算,并在此基础上将客户路由到多个潜在的选项中,所有这些都会导致诸如数据库条目之类的需求语句。
Then, within the db entry (or some other action) if everything goes smoothly, the member may have chosen before to receive an email confirmation based on his/her action. This require statement lies within the 2nd require (db insert) but is in a different directory than the second and thus causing conflict given the first file treats the linking incorrectly.
然后,在 db 条目(或某些其他操作)中,如果一切顺利,会员可能之前已经选择接收基于他/她的操作的电子邮件确认。这个 require 语句位于第二个 require (db insert) 中,但位于与第二个不同的目录中,因此在第一个文件错误地处理链接的情况下导致冲突。
Hard coding the absolute path or even setting the appropriate include path per page is 'ok' but then it disables our team's ability to hotlink between files with dreamweaver (or any other program that does the same) because it does not recognize a 'site root' when running in a test environment.
硬编码绝对路径或什至为每页设置适当的包含路径是“可以”的,但它会禁用我们的团队使用 Dreamweaver(或任何其他具有相同功能的程序)在文件之间进行热链接的能力,因为它无法识别“站点根目录” ' 在测试环境中运行时。
采纳答案by Andrew Moore
Your path is preceded by a forward slash (/
).
您的路径前面有一个正斜杠 ( /
)。
In a POSIX compliant system, if you have a path that starts with a forward slash, it means that it is an absolute path. The first forward slash represents the root of the filesystem.
在符合 POSIX 的系统中,如果您的路径以正斜杠开头,则表示它是绝对路径。第一个正斜杠代表文件系统的根。
Remove the forward slash from your path and you should then have a path that is relative to page.php
.
从您的路径中删除正斜杠,然后您应该有一个相对于page.php
.
EDIT:Since relative paths won't work, you can use dirname(__FILE__)
to get the absolute path of the directory where the current file resides.
编辑:由于相对路径不起作用,您可以使用dirname(__FILE__)
获取当前文件所在目录的绝对路径。
require(dirname(__FILE__) . '/includes/sess-start.php');
回答by mario
You need to use a relative path. When your filenames start with /
PHP will assume you mean the root directory. The correct prefix is ./
您需要使用相对路径。当您的文件名以/
PHP开头时,将假定您指的是根目录。正确的前缀是./
include('./includes/sess-start.php');
If you want all your paths to be relative to the document root, then this is a common method:
如果您希望所有路径都相对于文档根目录,那么这是一种常用方法:
include("$_SERVER[DOCUMENT_ROOT]/includes/sess-start.php");
回答by Matthew
Looks like you need to remove the leading slash from the include line.
看起来您需要从包含行中删除前导斜杠。
Edit: To address your concerns. In a prepend file or some common include, create a constant like DOCROOT
. You can dynamically determine it from your __FILE__
constant.
编辑:解决您的疑虑。在前置文件或一些常见的包含中,创建一个常量,如DOCROOT
. 您可以从__FILE__
常量动态确定它。
Then:
然后:
include(DOCROOT.'myfile.php');
Personally, I would try to set things up to avoid this sort of thing.
就个人而言,我会尝试进行设置以避免此类事情。