fopen()无法打开流:权限被拒绝,但权限应该有效



所以,我有这个错误:

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和切诺基进行了尝试,所以不可能是这样。

有某种解决方案吗?

编辑

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

更新-PHP fopen()

$fullpath = $this->fileRoot . $this->fileInData['fileName'];
$file_ptr = fopen( $fullpath, 'r+' );

我还应该提到,如果可能的话,我想继续使用切诺基。关于为Apache/Cherokee设置文件权限,这是怎么回事?

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

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

  • /path
  • /path/to

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

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

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

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

这帮了我一把。

最新更新