【问题标题】:fopen() fails to open stream: permission denied, yet permissions should be validfopen() 无法打开流:权限被拒绝,但权限应该是有效的
【发布时间】:2012-08-31 22:23:33
【问题描述】:

所以,我有这个错误:

Warning: fopen(/path/to/test-in.txt) [function.fopen]: failed to open stream: Permission denied

test-in.txt所在的目录中执行ls -l会产生以下输出:

-rw-r--r-- 1 $USER $USER 1921 Sep  6 20:09 test-in.txt
-rw-r--r-- 1 $USER $USER    0 Sep  6 20:08 test-out.txt

为了解决这个问题,我决定执行以下操作:

chgrp -R www-data /path/to/php/webroot

然后做了:

chmod g+rw /path/to/php/webroot

然而,当我运行 php5 脚本打开文件时,我仍然收到此错误。为什么会这样?我已经通过 CGI 使用 LAMP 和 cherokee 尝试过这个,所以不可能是这样。

有什么解决办法吗?

编辑

我还要补充一点,我现在只是通过 localhost 进行开发。

更新 - PHP fopen()

$fullpath = $this->fileRoot . $this->fileInData['fileName'];

$file_ptr = fopen( $fullpath, 'r+' );

我还应该提到,如果可能的话,我想坚持使用 Cherokee。为 Apache/Cherokee 设置文件权限有什么用?

【问题讨论】:

  • 网络服务器是以什么用户身份运行的,是否有打开文件的权限?
  • 它以我的用户身份运行。至于第二个,我可以cat 文本文件没有任何错误,如果有帮助的话。
  • 你确定 - 端口 80 是一个特权端口,我怀疑你可以打开它。请同时添加 PHP 行
  • 你能写下真实文件路径上每个目录的权限吗?
  • 更改权限即可。在 linux 主机中右键单击该文件,然后单击更改权限。然后启用所有权限。

标签: php linux permissions io fopen


【解决方案1】:

检查运行 PHP 的用户是否对文件路径的每个目录都具有“X”权限。
它将需要它来访问文件

如果您的文件是:/path/to/test-in.txt
你应该有 X 权限:

  • /path
  • /path/to

/path/to/test-in.txt的读取权限

【讨论】:

  • 是的,做到了。谢谢。看起来chgrp -R <group> </path/to/dir> 没有我想象的那么彻底……
【解决方案2】:

此错误的另一个原因可能是文件目录不存在。

我只是在fopen之前添加php代码:

if(!file_exists(dirname($file))) 
    mkdir(dirname($file));

这对我有帮助。

【讨论】:

    猜你喜欢
    • 2015-02-01
    • 1970-01-01
    • 2019-05-02
    • 2016-02-16
    • 1970-01-01
    • 2018-07-23
    • 2016-06-13
    • 2013-03-21
    • 1970-01-01
    相关资源
    最近更新 更多