fopen Permission denied on a file with 777 permiss

2020-02-13 23:27发布

Since the title of this post is pretty much self-explanatory, I'll just jump to the code :

echo sprintf('%o', fileperms('test.txt'))."<br/>";

fopen("test.txt", "w");

And with this I get :

100777
fopen(test.txt): failed to open stream: Permission denied

Any ideas ?

Edit : Problem solved : there were access control lists on the server that were not configured correctly.

Thanks !

4条回答
Evening l夕情丶
2楼-- · 2020-02-13 23:38

I had same issue: folder was 777, but fopen does not worked. fopen said permission deny. Make sure your script have a 'good' permissions. maybe it will help you:

echo $dst, file_exists($dst) ? ' exists' : ' does not exist', "\n";
echo $dst, is_readable($dst) ? ' is readable' : ' is NOT readable', "\n";
echo $dst, is_writable($dst) ? ' is writable' : ' is NOT writable', "\n";
$fh = fopen($dst, 'w');
if ( !$fh ) {
    echo ' last error: ';
    var_dump(error_get_last());
}
查看更多
在下西门庆
3楼-- · 2020-02-13 23:42

I just ran into this issue, and unfortunately the error message provided no clue to the actual reason. I had to give 777 to the file of the class included in the file that gave the error message. The error message only said the php file that calls that class, which already had 777.

So, check the file that is mentioned in the error message (let's say index.php), and then check which classes are instantiated within that file (class-file.php, class-writer.php, etc). Then check the permissions of those files (class-file.php, class-writer.php).

Once I gave permissions for the class file, it worked normally. Perhaps the webhost changed something in their config, since everything worked until a few days ago.

查看更多
smile是对你的礼貌
4楼-- · 2020-02-13 23:50

I think the problem you are having is file ownership issue ... you can use this to find out the problem

error_reporting(E_ALL);
ini_set('display_errors','On');

$file = "a.jpg";

echo sprintf ( '%o', fileperms ( $file ) ), PHP_EOL;
echo posix_getpwuid ( fileowner ( $file ) ), PHP_EOL; // Get Owner
echo posix_getpwuid ( posix_getuid () ), PHP_EOL; // Get User

if (is_file ( $file )) {
    echo "is_file", PHP_EOL;
    ;
}

if (is_readable ( $file )) {
    echo "is_readable", PHP_EOL;
    ;
}

if (is_writable ( $file )) {
    echo "is_readable", PHP_EOL;
}

fopen ( $file, "w" );
查看更多
放我归山
5楼-- · 2020-02-13 23:55

I think its possible that you have write/read permissions on the file but not on the folder. Try this in the public root of your website and see if you can read or write the file.

For safe mode (http://php.net/manual/en/function.fopen.php), php doc's say the following:

Note: When safe mode is enabled, PHP checks whether the directory in which the script is operating has the same UID (owner) as the script that is being executed.

Last you also need to be sure that php has access to the folder you are trying to write to.

查看更多
登录 后发表回答