2016-10-09 55 views
0

我被困在试图用php打开一个文件与fopen。PHP无法读取文件

 $db_ausgaenge = "statuseing.php"; 
     $dout = fopen($db_ausgaenge, "x+"); 
     print $dout; 
     print(shell_exec('whoami')); 
     print(shell_exec('pwd')); 
     print(shell_exec('id')); 
     fwrite($dout, $out); 
     fclose($dout); 

Warning: fopen(statuseing.php): failed to open stream: File exists in /var/www/html/zufallsgenerator.php on line 33 

我检查以下项目:

  • CHMOD为statuseing.php 0777
  • 所有者是www数据与对地表www数据
  • 脚本运行作为用户WWW的数据
  • groups is uid = 33(www-data)gid = 33(www-data)groups = 33(www-data)
  • pwd是/ var/www/html
  • 脚本想要打开的路径是正确的
  • 在php.ini中检查openbase dir显示在phpinfo()中,添加了/ var/www/html,但php并不关心它。

    的open_basedir =在/ var/www/html等/

通过systemctl什么守护重载和重新启动的Apache2改变后,phpinfo()函数没有显示在配置中给出的路径。通过init 6重新启动系统也没有生效。

回答

1

statuseing.php已经存在。

请参阅手册(http://php.net/manual/en/function.fopen.php) - 开放的X或X +模式说:Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE

+0

换开的方法来 'W +',现在它的正常工作。感谢提到这个问题。 –

+0

虽然这不是你的问题,但我个人总是喜欢在调用fopen()或类似的函数之前设置文件的完整路径。 –

0

试试这个:

$db_ausgaenge = __DIR__."/statuseing.php"; 
    $dout = fopen($db_ausgaenge, "a+"); // x+ will throw error cuz it tries to open existing file, thx to: bluegman991 (; 
    print(shell_exec('whoami')); 
    print(shell_exec('pwd')); 
    print(shell_exec('id')); 
    fwrite($dout, $out); 
    fclose($dout); 

,或者如果你想添加的数据使用w+前截断文件:

$db_ausgaenge = __DIR__."/statuseing.php"; 
    $dout = fopen($db_ausgaenge, "w+"); 
    print(shell_exec('whoami')); 
    print(shell_exec('pwd')); 
    print(shell_exec('id')); 
    fwrite($dout, $out); 
    fclose($dout); 

也做一些检查:

1)检查自由空间:df -h
2)检查是否可以编辑该文件:nano /var/www/html/statuseing.php