【问题标题】:How to set default permissions for new files created with php如何为使用 php 创建的新文件设置默认权限
【发布时间】:2013-05-16 08:43:16
【问题描述】:

您好,我正在运行一个 centos 服务器,我想知道如何设置由 php 创建的新文件(如 fopen)的默认 chmod。目前它正在执行 644 但我想要 666 那么我在哪里可以指定此设置?

【问题讨论】:

    标签: php umask


    【解决方案1】:

    您可以在 fopen() 调用之前立即使用 umask(),但如果您在多线程服务器上,则不应使用 umask - 它会更改所有线程的掩码(例如,此更改位于进程级别),而不仅仅是您将要在其中使用 fopen() 的那个。

    例如

    $old = umask(000);
    fopen('foo.txt', 'w'); // creates a 0666 file
    umask($old) // restore original mask
    

    然而,事后简单地 chmod() 会更容易:

    fopen('foo.txt', 'w'); // create a mode 'who cares?' file
    chmod('foo.txt', 0666); // set it to 0666
    

    【讨论】:

    • 当我想恢复当前脚本的原始掩码时,我只需要umask($old),对吧?或者umask 调用是否会进行永久性更改?
    【解决方案2】:

    与 Linux 一样,PHP 有一个 chmod() 命令,可以调用该命令来更改文件权限。

    在此处查看文档:http://php.net/manual/en/function.chmod.php

    对于默认设置,您可以尝试Patrick Fisher 在此处声明的内容:Setting the umask of the Apache user

    [root ~]$ echo "umask 000" >> /etc/sysconfig/httpd
    [root ~]$ service httpd restart
    

    【讨论】:

    • 是的,我知道这一点,但我说的是默认值而不更改它:)
    • 我进行了编辑以更好地解决您的问题。这应该适用于 Centos。
    猜你喜欢
    • 2017-04-04
    • 2012-04-11
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 2016-10-14
    • 2014-06-05
    相关资源
    最近更新 更多