php mkdir 0755失败,php-mkdir()创建0755而不是0775

我有以下代码:

function makedirs($dirpath, $mode = 0775, $recursive = true) {

return is_dir($dirpath) || mkdir($dirpath, $mode, $recursive);

}

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

makedirs($dir, 0775);

问题是:即使将0775或其他任何内容作为$mode的参数传递,mkdir()也会创建0755许可文件夹.

例如,previows代码将返回:

>路径/(0755)

>至/(0755)

>文件夹/(0755)

解决方法:

您可以执行以下操作

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);

注意:尽管您可以使用umask(0)甚至允许777权限,但不建议这样做,因为它可能会带来安全问题.

编辑

尝试为所有用户或您自己设置umask值系统范围,以从php中删除umask代码.

尽管上面的代码可以工作,但是不建议在php脚本中设置umask.

根据PHP手册页

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.

您可以在/ etc / bashrc或/ etc / profile文件中为所有用户设置umask.

默认情况下,大多数Linux发行版将其设置为0022(022)或0002(002).打开/ etc / profile或〜/ .bashrc文件,输入:

#vi / etc / profile

要么

$vi〜/ .bashrc

追加/修改以下行以设置新的umask:

umask 022

保存并关闭文件.更改将在下次登录后生效.所有UNIX用户都可以在其/ etc / profile文件,〜/ .profile(Korn / Bourne shell),〜/ .cshrc文件(C shell),〜/ .bash_profile(Bash shell)或〜/ .login中覆盖系统umask缺省值.文件(在登录时定义用户的环境).

标签:mkdir,chmod,file-permissions,php

来源: https://codeday.me/bug/20191118/2030693.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值