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

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) } 

图片转代码服务由CSDN问答提供 功能建议

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

  fopen(test.txt):无法打开流:权限被拒绝
   
 
 

请查看详细信息: \ n

  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.Tried setting : 已经尝试过的解决方案: fopen文件权限被拒绝 拥有777权限

    操作系统:Redhat Enterprise

    代码:

     &lt  ;?php 
    echo getmyuid()。':'。getmygid();  
    echo“&lt; br /&gt;”; 
    echo exec('whoami'); 
    echo“&lt; br /&gt;”; 
     $ dst ='test.txt'; 
    echo $ dst,file_exists($  dst)?  'exists':'不存在&lt; br /&gt;',“&lt; br /&gt; 
    ”; 
    echo $ dst,is_readable($ dst)?  '可读':'不可读',“&lt; br /&gt; 
    ”; 
    echo $ dst,is_writable($ dst)?  '是可写':'不可写&lt; br /&gt;',“&lt; br /&gt; 
    ”; 
     $ fh = fopen($ dst,'w'); 
    if(!$ fh){  
     echo'last error:'; 
     var_dump(error_get_last()); 
    } 
       
     
     

    输出:

      33:33 
    apache 
    test.txt存在
    test.txt是可读的
    test.txt是不可写的
     
    last error:array(4){[“type”] =&gt;  int(2)[“message”] =&gt;  string(57)“fopen(test.txt):无法打开流:权限被拒绝”[“file”] =&gt;  string(34)“/ var / www / data / test2.php”[“line”] =&gt;  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/
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题