1

我正在执行从php 5.6.40(Apache 2.2.15 CentOS 6.10 服务器)到php 8.0.1(Apache 2.4.37 CentOS 8.3)的 PHP 脚本迁移。

我遇到fopen()了根据模式选项引发警告的功能。我在网上搜索了没有发现类似的问题...

在每台服务器上,我都有相同的上下文和相同的权限:

$ pwd
/export/php/logs/aita
$ ll
-rw-r--r--. 1 apache apache     33 27 janv. 15:30 testFopen.txt

我的测试脚本非常简单,它检查所有 fopen 模式:

<?php
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'r');  // line 2
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'r+'); // line 3
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'w');  // line 4
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'w+'); // line 5
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'a');  // line 6
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'a+'); // line 7
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'x');  // line 8
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'x+'); // line 9
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'c');  // line 10
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'c+'); // line 11

运行此脚本时,我获得:

在我的旧服务器 PHP 5.6.40

[error] [client x.x.x.x] PHP Warning:  fopen(/export/php/logs/aita/testFopen.txt): failed to open stream: File exists in /export/html/aita_dev/testFopen.php on line 8
[error] [client x.x.x.x] PHP Warning:  fopen(/export/php/logs/aita/testFopen.txt): failed to open stream: File exists in /export/html/aita_dev/testFopen.php on line 9

这些警告是合乎逻辑的(文件存在和排除)。

在我的新服务器 PHP 8.0.1

[proxy_fcgi:error] [pid 37251:tid xxxx] [client x.x.x.x:x] AH01071: Got error '
PHP message: PHP Warning:  fopen(/export/php/logs/aita/testFopen.txt): Failed to open stream: Permission denied in /var/www/html/aita_dev/testFopen.php on line 3
PHP message: PHP Warning:  fopen(/export/php/logs/aita/testFopen.txt): Failed to open stream: Permission denied in /var/www/html/aita_dev/testFopen.php on line 4
PHP message: PHP Warning:  fopen(/export/php/logs/aita/testFopen.txt): Failed to open stream: Permission denied in /var/www/html/aita_dev/testFopen.php on line 5
PHP message: PHP Warning:  fopen(/export/php/logs/aita/testFopen.txt): Failed to open stream: File exists in /var/www/html/aita_dev/testFopen.php on line 8
PHP message: PHP Warning:  fopen(/export/php/logs/aita/testFopen.txt): Failed to open stream: File exists in /var/www/html/aita_dev/testFopen.php on line 9
PHP message: PHP Warning:  fopen(/export/php/logs/aita/testFopen.txt): Failed to open stream: Permission denied in /var/www/html/aita_dev/testFopen.php on line 10
PHP message: PHP Warning:  fopen(/export/php/logs/aita/testFopen.txt): Failed to open stream: Permission denied in /var/www/html/aita_dev/testFopen.php on line 11'

第 8 行和第 9 行的警告在这里也很有意义。

模式 r+、w[+] 和 c[+] 生成“权限被拒绝”警告。

模式 r, a[+] 运行良好,没有警告。

我可以使用“r”选项读取文件。我可以使用“a”和“a+”选项来编写文件,所以它看起来不像是权限问题。

我想了解并且不必更正我的应用程序的所有 fopen() ...

4

1 回答 1

1

负责服务器的管理员已降低 SELinux(Security-Enhanced Linux)的警报级别,现在一切正常。

按照 IMSoP 的建议,系统设置为仅允许 Apache 用户读取/附加文件

于 2021-01-28T14:41:22.350 回答