权限已设置为777,但文件仍无法写入。

6
我已将文件权限设置为777,但我无法使用PHP写入文件。
我在FTP客户端中清楚地看到该文件具有0777权限,当我执行以下操作时:
echo (true === is_writable('file.txt')) ? 'yes' : 'no';

我得到了“no”;

我还尝试了以下方法:

echo (true === chmod('file.txt', 0777)) ? 'yes' : 'no';

使用相同的结果。

目录列表大致如下:

public_html
    public          0777
        css         0755
        js          0755
        file.txt    0777

我正在使用 .htaccess 文件将所有流量重定向到 public 子文件夹。当然,我已经排除了该文件的重写(它可以从浏览器中访问,我已经检查过了):

RewriteRule  ^(file).*  - [L]

为什么会这样呢?

你有这个文件夹的写入权限吗? - Re0sless
你能提供该文件的目录清单吗?你可能不是所有者吗?此外,这可能是一个FTP服务器配置问题。 - ryanday
你为什么要使用 === 运算符? - Daniel A. White
你能否使用普通文本编辑器或终端命令(touch)来写入文件? - Peter Di Cecco
@Daniel A. White:=== 运算符还检查类型相等性,既然我们已经知道 is_writable 返回一个布尔值,那么使用 == 应该有相同的效果。 - Peter Di Cecco
显示剩余2条评论
4个回答

8
我猜Apache运行的用户/组与拥有该文件的用户/组不同。在这种情况下,文件本身需要是0777
如果您计划使用PHP向文件夹添加文件,则public只需要是0777。即使文件夹本身不是0777,如果文件是,而且文件夹至少有5个用户(读/执行),则应该能够写入文件。
最终,您的文件树应该如下所示:
public_html
    public
        file.txt  0777

自然地,您无法使用PHP更改这些权限,但是您可以从FTP客户端进行更改。

如果仍然无法正常工作,则可能是PHP运行在安全模式下或者您正在使用像PHP Suhosin这样的扩展。更改文件所有者为运行脚本的相同用户/组可能会得到更好的结果。

要获取执行用户的用户/组ID,您可以使用以下内容:

<?php
echo getmyuid().':'.getmygid(); //ex:. 0:0
?>

然后,您可以使用终端中的chown命令更改文件的所有者:

> chown 0:0 file.txt

实际上这是一个共享托管环境,所以我无法访问shell。但无论如何,感谢你非常有帮助的答案。我已经决定修改我的脚本,使其使用数据库来写入数据,而不是文本文件。现在它可以正常工作了。 - Richard Knop
2
我只想补充一下,我已经设置了正确的所有者,但我的文件仍然无法写入。原因是SELinux,正如在这个答案中所解释的那样:https://dev59.com/Z2HVa4cB1Zd3GeqPqtQb#9819118 - mkutyba

2
在安装vqmod并给予所有必要权限后,我在opencart中遇到了这个错误。
经过一番研究,我找到了解决方法。
实际上,“MODS CACHE PATH NOT WRITEABLE”指的是vqmod文件夹本身,而不是缓存文件夹。
sudo chmod -R 777 vqmod 

今日免费次数已满, 请开通会员/明日再来

1

在创建文件后,你必须使用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;
}

我上传了文件。我没有用PHP创建它。 - Richard Knop
1
请执行以下操作:var_dump(get_current_user()); 当前用户是否与上传文件的FTP用户相同?如果不是,那就是你的问题,你需要使用FTP客户端进行CHMOD,然后你就可以使用我上面发布的函数(或手动CHMOD)。 - Alix Axel

-1

虽然与上面的问题没有直接关系,但我会将它放在这里供未来的访问者参考,因为我是通过标题进入这里的。 来自arch wiki

File attributes

Apart from the file mode bits that control user and group read, write and execute permissions, several file systems support file attributes that enable further customization of allowable file operations. This section describes some of these attributes and how to work with them.

Warning: By default, file attributes are not preserved by cp, rsync, and other similar programs. chattr and lsattr

For ext2 and ext3 file systems, the e2fsprogs package contains the programs lsattr and chattr that list and change a file's attributes, respectively. Though some are not honored by all file systems, the available attributes are:

a: append only
c: compressed
d: no dump
e: extent format
i: immutable
j: data journalling
s: secure deletion
t: no tail-merging
u: undeletable
A: no atime updates
C: no copy on write
D: synchronous directory updates
S: synchronous updates
T: top of directory hierarchy

For example, if you want to set the immutable bit on some file, use the following command:

# chattr +i /path/to/file

To remove an attribute on a file just change + to -.

对于没有附加属性的文件,lsattr 的输出如下:

$ lsattr /etc/hosts
-------------e- /etc/hosts

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接