dto5637 2018-03-07 08:43
浏览 82
已采纳

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

In a PHP script , am getting below error when checked if a file is writable or not.

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

Please see blow details:

  1. Apache service is running as user:apache, checked with:

    ps aux | egrep '(apache|httpd)'

2.Ownership of the file to be written and php script is changed to apache.

chown apache:apache test.txt 
chown apache:apache test.php

3.File permissions changed to:777

chmod 777 test.txt 
chmod 777 test.php

4.Tried setting: Already tried solutions provided in:fopen Permission denied on a file with 777 permissions

OS:Redhat Enterprise

Code:

<?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/>
";
echo $dst, is_readable($dst) ? ' is readable' : ' is NOT readable', "<br/>
";
echo $dst, is_writable($dst) ? ' is writable' : ' is NOT writable<br/>', "<br/>
";
$fh = fopen($dst, 'w');
if ( !$fh ) {
    echo ' last error: ';
    var_dump(error_get_last());
}

Output:

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) } 
  • 写回答

1条回答 默认 最新

  • douji8033 2018-03-07 14:58
    关注

    At last reached the answer myself! I had to modify the SELinux configuration with chon command and apply permission as:httpd_sys_rw_content_t

    chcon -R -h -t httpd_sys_rw_content_t /var/www/data/
    

    along with:

    chown -R apache:apache/var/www/data/
    chmod -R a+rX /var/www/data/
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)