简体   繁体   English

fopen对具有777权限的文件的权限被拒绝

[英]fopen Permission denied on a file with 777 permissions

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 ! 谢谢 !

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: 对于安全模式( http://php.net/manual/en/function.fopen.php),php doc说如下:

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. 注意:启用安全模式后,PHP会检查脚本运行的目录是否与正在执行的脚本具有相同的UID(所有者)。

Last you also need to be sure that php has access to the folder you are trying to write to. 最后,您还需要确保php可以访问您尝试写入的文件夹。

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" );

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. 我必须将777提供给包含在提供错误消息的文件中的类的文件。 The error message only said the php file that calls that class, which already had 777. 错误消息只表示调用该类的php文件,该文件已经有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). 因此,检查错误消息中提到的文件(比如index.php),然后检查在该文件中实例化的类(class-file.php,class-writer.php等)。 Then check the permissions of those files (class-file.php, class-writer.php). 然后检查这些文件的权限(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. 也许webhost在他们的配置中改变了一些东西,因为一切都工作到几天前。

I had same issue: folder was 777, but fopen does not worked. 我有同样的问题:文件夹是777,但fopen不起作用。 fopen said permission deny. fopen说允许否认。 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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM