php 权限设置为 777 并且文件仍然不可写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1260150/
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
Permissions set to 777 and file still not writeable
提问by Richard Knop
I have set file permissions to 777 yet I cannot write to the file with PHP.
我已将文件权限设置为 777,但无法使用 PHP 写入文件。
I can clearly see in my FTP client that the file has 0777 permissions and when I do:
我可以在我的 FTP 客户端中清楚地看到该文件具有 0777 权限,当我这样做时:
echo (true === is_writable('file.txt')) ? 'yes' : 'no';
I get 'no';
我得到“不”;
I also tried:
我也试过:
echo (true === chmod('file.txt', 0777)) ? 'yes' : 'no';
With the same result.
同样的结果。
The directory listing goes something like this:
目录列表是这样的:
public_html
public 0777
css 0755
js 0755
file.txt 0777
And I'm using .htaccess file to redirect all traffic to the public subfolder. Of course, I have excluded the file from rewriting (it is accessible from the browser I checked):
我正在使用 .htaccess 文件将所有流量重定向到公共子文件夹。当然,我已经从重写中排除了该文件(可以从我检查的浏览器中访问它):
RewriteRule ^(file).* - [L]
Why is that?
这是为什么?
回答by Andrew Moore
I guess Apache runs as a different user/group than the user/group owning the file. In which case, the file itself needs to be 0777.
我猜 Apache 以与拥有该文件的用户/组不同的用户/组身份运行。在这种情况下,文件本身需要是0777.
publiconly needs to be 0777if you plan on adding files to the folder using PHP. Even if the folder itself is not 0777, if the file is and the folder has at least 5for the user (read/execute), you should be able to write to the file.
public仅0777当您计划使用 PHP 将文件添加到文件夹时才需要。即使文件夹本身不是0777,如果文件是并且文件夹至少5有用户 ( read/execute),您应该能够写入文件。
In the end, your file tree should look like this:
最后,您的文件树应如下所示:
public_html
public
file.txt 0777
Naturally, you won't be able to change those permissions using PHP, but you can do so from your FTP client.
当然,您将无法使用 PHP 更改这些权限,但您可以通过 FTP 客户端进行更改。
If it still isn't working, PHP might be running in safe mode or you might be using an extension such as PHP Suhosin. You might get better result changing the owner of the file to the same user/group that is running the script.
如果它仍然不起作用,则 PHP 可能正在安全模式下运行,或者您可能正在使用诸如 PHP Suhosin 之类的扩展程序。将文件的所有者更改为运行脚本的同一用户/组可能会获得更好的结果。
To get the user/group id of the executing user, you may use the following:
要获取执行用户的用户/组 ID,您可以使用以下命令:
<?php
echo getmyuid().':'.getmygid(); //ex:. 0:0
?>
Then, you may use chown(in the terminal) to change the owner of the file:
然后,您可以使用chown(在终端中)更改文件的所有者:
> chown 0:0 file.txt
回答by Pavan Mehta
In opencart i faced this error after installing vqmod and giving all necessary permissions.
在 opencart 中,我在安装 vqmod 并授予所有必要的权限后遇到了这个错误。
after researching a bit, found it.
经过一番研究,找到了。
"MODS CACHE PATH NOT WRITEABLE" is actually refering to vqmod folder itself and not the cache folder.
“MODS CACHE PATH NOT WRITEABLE”实际上是指 vqmod 文件夹本身而不是缓存文件夹。
sudo chmod -R 777 vqmod
in your root directory.....
在你的根目录.....
回答by Alix Axel
You have to chmod the file right after you create it.
您必须在创建文件后立即对其进行 chmod。
function Doo_Chmod($path, $chmod = null)
{
if (file_exists($path) === true)
{
if (is_null($chmod) === true)
{
$chmod = (is_file($path) === true) ? 644 : 755;
if (in_array(get_current_user(), array('apache', 'httpd', 'nobody', 'system', 'webdaemon', 'www', 'www-data')) === true)
{
$chmod += 22;
}
}
return chmod($path, octdec(intval($chmod)));
}
return false;
}

