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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12311256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:16:54  来源:igfitidea点击:

fopen() fails to open stream: permission denied, yet permissions should be valid

phplinuxpermissionsiofopen

提问by zeboidlund

So, I have this error:

所以,我有这个错误:

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

Performing ls -lin the directory where test-in.txtis produces the following output:

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

-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

In order to get past this, I decided to perform the following:

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

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

And then did:

然后做了:

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

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

Yet, I still get this error when I run my php5 script to open the file. Why is this happening? I've tried this using LAMP as well as cherokee through CGI, so it can't be this.

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

Is there a solution of some sort?

是否有某种解决方案?

Edit

编辑

I'll also add that I'm just developing via localhost right now.

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

Update - PHP fopen()line

更新 - PHPfopen()

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

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

I should also mention I'd like to stick with Cherokee if possible. What's this deal about setting file permissions for Apache/Cherokee?

我还应该提到,如果可能的话,我想坚持使用切诺基。关于为 Apache/Cherokee 设置文件权限的交易是什么?

采纳答案by Izack

Check if the user that PHP runs under have "X" permission on every directory of the file path.
It will need it to access the file

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

If your file is: /path/to/test-in.txt
You should have X permission on:

如果您的文件是:/path/to/test-in.txt
您应该对以下内容拥有 X 权限:

  • /path
  • /path/to
  • /path
  • /path/to

and read permission on /path/to/test-in.txt

并阅读权限 /path/to/test-in.txt

回答by onebraveman

Another reason of this error may be that the directory of file does not exist.

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

I just add php code before fopen:

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

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

This help me out.

这帮我解决了。