【发布时间】:2018-08-15 07:03:12
【问题描述】:
在 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
操作系统:红帽企业
代码:
<?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) }
【问题讨论】:
-
您存放这些文件的文件夹是否也归 apache 所有?
-
您要修改的文件/文件夹需要写权限,一直到
/也需要读权限。没有更多信息,不能多说。 -
权限存在:chmod 777 test.txt chmod 777 test.php
-
或者您的 PHP 没有尝试打开您认为它正在尝试打开的文件。
-
在某些配置中,777 权限实际上会阻止您访问或写入文件。你能阅读 test.txt 吗? (即使用 wget/浏览器)
标签: php apache httpd.conf