duanjie1339 2016-04-20 14:39
浏览 556
已采纳

mkdir()创建0755而不是0775

I have the following code:

function makedirs($dirpath, $mode = 0775, $recursive = true) {
    return is_dir($dirpath) || mkdir($dirpath, $mode, $recursive);
}

$dir = 'path/to/folder/';

makedirs($dir, 0775);

The problem is: even when passing 0775 or anything else as parameter for $mode, mkdir() creates 0755 permition folders.

For exemple the previows code will return:

  • path/ (0755)
  • to/ (0755)
  • folder/ (0755)
  • 写回答

1条回答 默认 最新

  • douzhan8652 2016-04-20 14:59
    关注

    You could do the following

    function makedirs($dirpath, $mode = 0775, $recursive = true) {
        $oldMask=umask(002);
        $status = is_dir($dirpath) || mkdir($dirpath, $mode, $recursive);
        umask($oldMask);
        return $status;
    }
    
    $dir = 'path/to/folder/'
    makedirs($dir, 0775);
    

    Note : Although you could use umask(0) to allow even 777 permissions It's not recommended as it could pose security issues.

    Edit

    Try setting the umask value system wide for all users or for yourself to remove the umask code from php. Although the above code would work, setting umask in php scripts is not recommended.

    According to the PHP Manual Page

    Avoid using this function in multithreaded webservers. It is better to change the file permissions with chmod() after creating the file. Using umask() can lead to unexpected behavior of concurrently running scripts and the webserver itself because they all use the same umask.

    You can setup umask in /etc/bashrc or /etc/profile file for all users. By default most Linux distro set it to 0022 (022) or 0002 (002). Open /etc/profile or ~/.bashrc file, enter:

    # vi /etc/profile

    OR

    $ vi ~/.bashrc

    Append/modify following line to setup a new umask: umask 022

    Save and close the file. Changes will take effect after next login. All UNIX users can override the system umask defaults in their /etc/profile file, ~/.profile (Korn / Bourne shell) ~/.cshrc file (C shells), ~/.bash_profile (Bash shell) or ~/.login file (defines the user’s environment at login).

    Source http://www.cyberciti.biz/tips/understanding-linux-unix-umask-value-usage.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!