更新时间:2022-06-15 23:13:14
有很多原因可能会导致此错误,因此良好的清单首先要检查的内容很有帮助.
There are many reasons why one might run into this error and thus a good checklist of what to check first helps considerably.
让我们考虑对以下行进行故障排除:
Let's consider that we are troubleshooting the following line:
require "/path/to/file"
或将require*
或include*
调用的内容移至其自己的变量,将其回显,复制并尝试从终端进行访问:
or move whatever is called by require*
or include*
to its own variable, echo it, copy it, and try accessing it from a terminal:
$path = "/path/to/file";
echo "Path : $path";
require "$path";
然后,在终端中:
cat <file path pasted>
/users/tony/htdocs
/users/tony/htdocs
***做法:
为了使您的脚本健壮,以防万一,您仍然可以在运行时生成绝对路径,同时有两个选择:
In order to make your script robust in case you move things around, while still generating an absolute path at runtime, you have 2 options :
require __DIR__ . "/relative/path/from/current/file"
. __DIR__
魔术常数返回当前文件的目录自己定义一个SITE_ROOT
常量:
require __DIR__ . "/relative/path/from/current/file"
. The __DIR__
magic constant returns the directory of the current file.define a SITE_ROOT
constant yourself :
config.php
在config.php
中编写
config.php
in config.php
, write
define('SITE_ROOT', __DIR__);
都包含config.php
,然后在任何需要的地方使用SITE_ROOT
常量:
in every file where you want to reference the site root folder, include config.php
, and then use the SITE_ROOT
constant wherever you like :
require_once __DIR__."/../config.php";
...
require_once SITE_ROOT."/other/file.php";
这2种做法还使您的应用程序更加可移植,因为它不依赖于ini设置(例如包含路径).
These 2 practices also make your application more portable because it does not rely on ini settings like the include path.
Another way to include files, neither relatively nor purely absolutely, is to rely on the include path. This is often the case for libraries or frameworks such as the Zend framework.
这样的包含将看起来像这样:
Such an inclusion will look like this :
include "Zend/Mail/Protocol/Imap.php"
在这种情况下,您将要确保"Zend"所在的文件夹是包含路径的一部分.
In that case, you will want to make sure that the folder where "Zend" is, is part of the include path.
您可以使用:
echo get_include_path();
您可以使用:
set_include_path(get_include_path().":"."/path/to/new/folder");
运行服务器进程(Apache或PHP)的用户可能根本没有权限读取或写入该文件.
It might be that all together, the user running the server process (Apache or PHP) simply doesn't have permission to read from or write to that file.
要检查服务器在哪个用户下运行,可以使用 posix_getpwuid :
To check under what user the server is running you can use posix_getpwuid :
$user = posix_getpwuid(posix_geteuid());
var_dump($user);
要查找文件的权限,请在终端中键入以下命令:
To find out the permissions on the file, type the following command in the terminal:
ls -l <path/to/file>
并查看权限符号表示法
如果以上方法均无效,则可能是某些PHP设置禁止其访问该文件.
If none of the above worked, then the issue is probably that some PHP settings forbid it to access that file.
三个设置可能是相关的:
Three settings could be relevant :
phpinfo()
进行检查使用ini_get("open_basedir")
phpinfo()
or by using ini_get("open_basedir")
ini_get("allow_url_include")
进行检查,并使用ini_set("allow_url_include", "1")
ini_get("allow_url_include")
and set with ini_set("allow_url_include", "1")
如果以上方法均无法诊断问题,则可能会出现一些特殊情况:
If none of the above enabled to diagnose the problem, here are some special situations that could happen :
您可能会使用相对或绝对路径包含一个库,例如Zend框架.例如:
It can happen that you include a library, for example, the Zend framework, using a relative or absolute path. For example :
require "/usr/share/php/libzend-framework-php/Zend/Mail/Protocol/Imap.php"
但是您仍然会遇到同样的错误.
But then you still get the same kind of error.
之所以会发生这种情况,是因为(成功地)包含了该文件,它本身具有另一个文件的include语句,并且第二个include语句假定您已将该库的路径添加到include路径中.
This could happen because the file that you have (successfully) included, has itself an include statement for another file, and that second include statement assumes that you have added the path of that library to the include path.
例如,前面提到的Zend框架文件可以包含以下内容:
For example, the Zend framework file mentioned before could have the following include :
include "Zend/Mail/Protocol/Exception.php"
既不是相对路径也不是绝对路径.假定Zend框架目录已添加到包含路径.
which is neither an inclusion by relative path, nor by absolute path. It is assuming that the Zend framework directory has been added to the include path.
在这种情况下,唯一可行的解决方案是将目录添加到您的包含路径中.
In such a case, the only practical solution is to add the directory to your include path.
如果您运行的是增强安全性的Linux,则可能是该问题的原因,它是通过拒绝从服务器访问文件来实现的.
If you are running Security-Enhanced Linux, then it might be the reason for the problem, by denying access to the file from the server.
要检查系统上是否已启用SELinux ,请在终端中运行sestatus
命令.如果该命令不存在,则SELinux不在您的系统上.如果确实存在,那么它应该告诉您是否强制执行.
To check whether SELinux is enabled on your system, run the sestatus
command in a terminal. If the command does not exist, then SELinux is not on your system. If it does exist, then it should tell you whether it is enforced or not.
要检查SELinux策略是否是造成此问题的原因,您可以尝试暂时将其关闭.但是请小心,因为这将完全禁用保护.不要在生产服务器上执行此操作.
To check whether SELinux policies are the reason for the problem, you can try turning it off temporarily. However be CAREFUL, since this will disable protection entirely. Do not do this on your production server.
setenforce 0
如果您再也没有关闭SELinux的问题,那么这就是根本原因.
If you no longer have the problem with SELinux turned off, then this is the root cause.
要解决此问题,您将必须相应地配置SELinux.
To solve it, you will have to configure SELinux accordingly.
以下上下文类型将是必需的:
The following context types will be necessary :
httpd_sys_content_t
表示您希望服务器能够读取的文件httpd_sys_rw_content_t
用于您要对其进行读写访问的文件httpd_log_t
用于日志文件httpd_cache_t
用于缓存目录httpd_sys_content_t
for files that you want your server to be able to readhttpd_sys_rw_content_t
for files on which you want read and write accesshttpd_log_t
for log fileshttpd_cache_t
for the cache directory例如,要将httpd_sys_content_t
上下文类型分配给您的网站根目录,请运行:
For example, to assign the httpd_sys_content_t
context type to your website root directory, run :
semanage fcontext -a -t httpd_sys_content_t "/path/to/root(/.*)?"
restorecon -Rv /path/to/root
如果文件位于主目录中,则还需要打开httpd_enable_homedirs
布尔值:
If your file is in a home directory, you will also need to turn on the httpd_enable_homedirs
boolean :
setsebool -P httpd_enable_homedirs 1
无论如何,根据您的策略,SELinux拒绝访问文件的原因可能有多种.因此,您需要对此进行调查. 此处是专门针对Web服务器配置SELinux的教程.
In any case, there could be a variety of reasons why SELinux would deny access to a file, depending on your policies. So you will need to enquire into that. Here is a tutorial specifically on configuring SELinux for a web server.
如果您正在使用Symfony,并且在上载到服务器时遇到此错误,则可能是由于app/cache
已被上载或者尚未清除缓存而未重置应用程序的缓存
If you are using Symfony, and experiencing this error when uploading to a server, then it can be that the app's cache hasn't been reset, either because app/cache
has been uploaded, or that cache hasn't been cleared.
您可以通过运行以下控制台命令来测试并解决此问题:
You can test and fix this by running the following console command:
cache:clear
显然,当zip中的某些文件的文件名中包含非ASCII字符(例如é")时,调用zip->close()
时也会发生此错误.
Apparently, this error can happen also upon calling zip->close()
when some files inside the zip have non-ASCII characters in their filename, such as "é".
潜在的解决方案是在创建目标文件之前将文件名包装在utf8_decode()
中.
A potential solution is to wrap the file name in utf8_decode()
before creating the target file.
向 Fran Cano 表示感谢,以标识并提出解决此问题的方法
Credits to Fran Cano for identifying and suggesting a solution to this issue