PHP:更改权限和所有权后文件不可写

标签 php apache httpd.conf

在 PHP 脚本中,检查文件是否可写时出现以下错误。

fopen(test.txt): failed to open stream: Permission denied

请参阅打击详情:

  1. 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/

相关文章:

performance - 在 apache2.conf 中包含带有 'AllowOverride None' 的 .htaccess 会减慢 Apache 的速度吗?

mysql - 如何在Windows后台运行apache的httpd守护进程?

linux - Apache2:尝试启动时出现 "Address already in use"('httpd.pid' 问题?)

PHPDOC:隐藏未记录成员的选项

php - 需要从输入表单设置数据库的文件路径

java - 无法在 apache tomcat 中使用 Context docBase 显示 html 图像

WordPress 管理员卡在 https,如何关闭它?

php - 检测匹配的高效算法

PHP + Python - 使用 PHP 将字符串传递给 Python 程序,并解析输出

linux - 确定 HTTPS apache 连接使用的密码强度