php PHP致命错误打开所需文件失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5364233/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:10:15  来源:igfitidea点击:

PHP Fatal Error Failed opening required File

phppath

提问by Al Katawazi

I am getting the following error from Apache

我从 Apache 收到以下错误

[Sat Mar 19 23:10:50 2011] [warn] mod_fcgid: stderr: PHP Fatal error: require_once() [function.require]: Failed opening required '/common/configs/config_templates.inc.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/viapics1/public_html/common/configs/config.inc.php on line 158

[Sat Mar 19 23:10:50 2011] [warn] mod_fcgid: stderr: PHP Fatal error: require_once() [function.require]: Failed opening required '/common/configs/config_templates.inc.php' (include_path=' .:/usr/share/pear:/usr/share/php') 在 /home/viapi​​cs1/public_html/common/configs/config.inc.php 第 158 行

I am definately not an expert of Apache but the file config.inc.php & config_templates.inc.php are there. I also tried navigating to a test.html page I placed in common/configs/ so I assume there is no rights issues going on. I also set the rights on config_templates.inc.php to give everyone read, write, and execute rights. Not sure what to do at this point, I checked to see if there was a /usr/share/php directory and I found there was not but when I did yum install php it said it had the latest. Ideas?

我绝对不是 Apache 的专家,但文件 config.inc.php 和 config_templates.inc.php 就在那里。我还尝试导航到放置在 common/configs/ 中的 test.html 页面,因此我认为没有发生任何权限问题。我还在 config_templates.inc.php 上设置了权限,给每个人读、写和执行的权限。不知道此时该做什么,我检查了是否有 /usr/share/php 目录,我发现没有,但是当我执行 yum install php 时,它说它有最新的。想法?

回答by Your Common Sense

It's not actually an Apache related question. Nor even a PHP related one. To understand this error you have to distinguish a path on the virtual serverfrom a path in the filesystem.

这实际上不是与 Apache 相关的问题。甚至没有一个 PHP 相关的。要理解此错误,您必须将虚拟服务器上的路径与文件系统中的路径区分开来。

requireoperator works with files. But there is no

require操作员处理文件。但是没有

                          /common/configs/config_templates.inc.php

file on the server, but rather

文件在服务器上,而是

/home/viapics1/public_html/common/configs/config_templates.inc.php

The

/home/viapics1/public_html

part is called the Document rootand it connects the virtual world with the real one. Luckily, web-servers usually have the document root in a configuration variable that they share with PHP. So if you change your code to something like

部分称为文档根,它将虚拟世界与真实世界连接起来。幸运的是,Web 服务器通常在与 PHP 共享的配置变量中具有文档根目录。因此,如果您将代码更改为类似

require_once $_SERVER['DOCUMENT_ROOT'].'/common/configs/config_templates.inc.php';

it will work from any file placed in any directory.

它适用于放置在任何目录中的任何文件。

Update: eventually I wrote an article that explains the difference between relative and absolute paths, in the file system and on the web server, which explains the matter in detail, and contains some practical solutions.

更新:最终我写了一篇文章,解释了相对路径和绝对路径之间区别,在文件系统和 Web 服务器上,详细解释了这个问题,并包含一些实用的解决方案。

Unfortunately, such a handy variable doesn't exist when you run your script from a command line. In this case a thechnique called "a single entry point" is to the rescue. You may refer to the article above for the details as well.

不幸的是,当您从命令行运行脚本时,这种方便的变量并不存在。在这种情况下,称为“单一入口点”的技术可以解决问题。您也可以参考上面的文章了解详细信息。

回答by user1533634

If you have SELinux running, you might have to grant httpd permission to read from /home dir using:

如果您正在运行 SELinux,则可能必须授予 httpd 权限以使用以下命令从 /home 目录读取:

 sudo setsebool httpd_read_user_content=1

回答by Alex Howansky

Run php -f /common/configs/config_templates.inc.phpto verify the validity of the PHP syntax in the file.

运行php -f /common/configs/config_templates.inc.php以验证文件中 PHP 语法的有效性。

回答by John Rix

Just in case this helps anybody else out there, I stumbled on an obscure case for this error triggering last night. Specifically, I was using the require_once method and specifying only a filename and no path, since the file being required was present in the same directory.

以防万一这对其他人有帮助,我偶然发现了昨晚触发此错误的一个不起眼的案例。具体来说,我使用了 require_once 方法并只指定了一个文件名而没有指定路径,因为所需的文件存在于同一目录中。

I started to get the 'Failed opening required file' error at one point. After tearing my hair out for a while, I finally noticed a PHP Warning message immediately above the fatal error output, indicating 'failed to open stream: Permission denied', but more importantly, informing me of the path to the file it was trying to open. I then twigged to the fact I had created a copy of the file (with ownership not accessible to Apache) elsewhere that happened to also be in the PHP 'include' search path, and ahead of the folder where I wanted it to be picked up. D'oh!

我开始收到“打开所需文件失败”错误。撕了一段时间后,我终于注意到致命错误输出正上方的一条 PHP 警告消息,表明“无法打开流:权限被拒绝”,但更重要的是,它告诉我它试图打开的文件的路径打开。然后我注意到我在其他地方创建了一个文件副本(Apache 无法访问其所有权),该副本恰好也在 PHP 的“包含”搜索路径中,并且位于我希望它被拾取的文件夹之前. 哦!

回答by Bernard

You could fix it with the PHP constant __DIR__

你可以用 PHP 常量修复它 __DIR__

require_once __DIR__ . '/common/configs/config_templates.inc.php';

It is the directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname __FILE__. This directory name does not have a trailing slash unless it is the root directory. 1

它是文件的目录。如果在包含中使用,则返回包含文件的目录。这相当于 dirname __FILE__。除非它是根目录,否则此目录名称没有尾部斜杠。1

回答by Sweet Chilly Philly

Hey I just had this problem and found that I wasn't looking at the folder location closely enough:

嘿,我刚遇到这个问题,发现我没有足够仔细地查看文件夹位置:

I had

我有

require_once /vagrant/public/liberate/**APP**/vendor/autoload.php

What worked was:

有效的是:

require_once /vagrant/public/liberate/vendor/autoload.php

It was very easy (as a beginner) to overlook this very unnoticeable issue. Yes I do realize that the require issue being logged points directly to the issue at hand, but if you are a beginner, like me, these things can be easily overlooked.

很容易(作为初学者)忽略这个非常不起眼的问题。是的,我确实意识到记录的 require 问题直接指向手头的问题,但是如果您是像我这样的初学者,这些事情很容易被忽略。

FIX:

使固定:

Have a good look at the debug of ( __ Dir __ '/etc/etc/etc/file.php') then have your file system open in a different window, and map the two directly. If there is even the slightest difference this require will not work and the above error will be spat out.

仔细查看 ( __ Dir __ '/etc/etc/etc/file.php') 的调试,然后在不同的窗口中打开您的文件系统,并直接映射两者。如果有最细微的差异,则此要求将不起作用,并且会吐出上述错误。

回答by Eduardo

I was having the exact same issue, I triple checked the include paths, I also checked that pear was installed and everything looked OK and I was still getting the errors, after a few hours of going crazy looking at this I realized that in my script had this:

我遇到了完全相同的问题,我三次检查了包含路径,我还检查了 pear 是否已安装,一切看起来都不错,但我仍然收到错误,经过几个小时的疯狂查看后,我意识到在我的脚本中有这个:

include_once "../Mail.php";

instead of:

代替:

include_once ("../Mail.php");

Yup, the stupid parenthesis was missing, but there was no generated error on this line of my script which was odd to me

是的,愚蠢的括号丢失了,但是我的脚本的这一行没有生成错误,这对我来说很奇怪