在 PHP 脚本中,检查文件是否可写时出现以下错误。
fopen(test.txt): failed to open stream: Permission denied
请参阅打击详情:
Apache 服务正在以用户身份运行:apache,检查方式为:
ps aux | egrep '(apache|httpd)'
2.将要写入的文件和php脚本的所有权更改为apache。
chown apache:apache test.txt
chown apache:apache test.php
3.文件权限改为:777
chmod 777 test.txt
chmod 777 test.php
4.尝试设置: 已经尝试过以下提供的解决方案:fopen Permission denied on a file with 777 permissions
操作系统:Redhat Enterprise
代码:
<?php
echo getmyuid().':'.getmygid();
echo "<br/>";
echo exec('whoami');
echo "<br/>";
$dst = 'test.txt';
echo $dst, file_exists($dst) ? ' exists' : ' does not exist<br/>', "<br/>\n";
echo $dst, is_readable($dst) ? ' is readable' : ' is NOT readable', "<br/>\n";
echo $dst, is_writable($dst) ? ' is writable' : ' is NOT writable<br/>', "<br/>\n";
$fh = fopen($dst, 'w');
if ( !$fh ) {
echo ' last error: ';
var_dump(error_get_last());
}
输出:
33:33
apache
test.txt exists
test.txt is readable
test.txt is NOT writable
last error: array(4) { ["type"]=> int(2) ["message"]=> string(57) "fopen(test.txt): failed to open stream: Permission denied" ["file"]=> string(34) "/var/www/data/test2.php" ["line"]=> int(10) }
最佳答案
我自己终于找到答案了!我必须使用 chon 命令修改 SELinux 配置并应用权限:httpd_sys_rw_content_t
chcon -R -h -t httpd_sys_rw_content_t /var/www/data/
连同:
chown -R apache:apache/var/www/data/
chmod -R a+rX /var/www/data/
关于PHP:更改权限和所有权后文件不可写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49147487/