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

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

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

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址