linux创建文件最大权限问题以及上传文件权限计算 php 波浪号 ~

2021年11月23日08:10:40

 

 

但是最近没有这么写PHP,突然看一段代码懵了

@chmod($target, 0666 & ~umask());

 

注意一下,linux的 ~符号是代表home目录

建议如果需要做文件上传的话,可以单独使用下面的代码,是laravel的官方文件上传

public function move(string $directory, string $name = null)
    {
        if ($this->isValid()) {
            if ($this->test) {
                return parent::move($directory, $name);
            }

            $target = $this->getTargetFile($directory, $name);

            set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
            $moved = move_uploaded_file($this->getPathname(), $target);
            restore_error_handler();
            if (!$moved) {
                throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s).', $this->getPathname(), $target, strip_tags($error)));
            }

            @chmod($target, 0666 & ~umask());

            return $target;
        }

        switch ($this->error) {
            case \UPLOAD_ERR_INI_SIZE:
                throw new IniSizeFileException($this->getErrorMessage());
            case \UPLOAD_ERR_FORM_SIZE:
                throw new FormSizeFileException($this->getErrorMessage());
            case \UPLOAD_ERR_PARTIAL:
                throw new PartialFileException($this->getErrorMessage());
            case \UPLOAD_ERR_NO_FILE:
                throw new NoFileException($this->getErrorMessage());
            case \UPLOAD_ERR_CANT_WRITE:
                throw new CannotWriteFileException($this->getErrorMessage());
            case \UPLOAD_ERR_NO_TMP_DIR:
                throw new NoTmpDirFileException($this->getErrorMessage());
            case \UPLOAD_ERR_EXTENSION:
                throw new ExtensionFileException($this->getErrorMessage());
        }

        throw new FileException($this->getErrorMessage());
    }
View Code

 

注意这个涉及到的一个最大问题就是

@chmod($target, 0666 & ~umask());

为什么要这么计算文件权限呢?

 

这个就要涉及到linux的权限体系

ll -a

-rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc
drwx------. 22 root root 4096 Nov 11 15:25 .cache

列表信息分为:

①文件类型、②权限、③连接数、④所属用户(所有者)、⑤所属用户组、⑥文件容量、⑦最后修改时间、⑧文件名

 

①文件类型

linux的7种文件类型

 

②文件权限

权限对文件及目录的作用:

 

 

 

权限的计算,这就是我们常看到 完整权限0777 就是我们常说的8进制

r 4
w 2
x 1
- 0

举例:

rw- --- ---

420 000 000

600

rw- r-- r–

420 420 420

644

rwx r-x r-x

421 401 401

755

 

 

 

 

③连接数:表述有多少文件名连接至此节点(i-node)

每个文件都会将它的权限与属性记录到文件系统的i-node中,我们使用的目录树是使用文件名来记录,因此每个文件名会连接到一个i-node。

④表示这个文件的所有者账号

⑤表示这个文件的所属用户组

⑥文件的容量大小,默认单位为B

⑦创建文件日期或者是最近的修改日期

根据文件修改日期的久远程度差异性显示,可使用“ls -l --full-time”或“ll -test.txt --full-time”显示完 整时间

⑧文件名,若文件名之前多一个‘.’则代表这个文件是隐藏文件

 

 

 

用vim编辑一个shell文字批处理文件通常是“664”。将该文件变成可执行文件后通常是“755”

 

 linux中控制默认系统权限的命令umask
根据umask计算出系统默认的权限
系统中文件最大的权限666,除非是脚本就要执行权限,
目录的最大的权限777

文件 666 022 644
根据umack计算文件的默认权限
666-022=644
目录 777 022 755
根据umask计算目录的默认权限
777 - 022 = 755

vi /etc/profile

if
[ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then umask 002 else umask 022 fi

 

1,umsak:user file-creation mode mask,是用户创建文件时默认权限的基础。
2,umask是一个需要对所有用户都生效的变量,所以需要在/etc/profile中设置。
3,没有umask时,文件的默认权限是0666(默认只用来读写),目录的默认权限是0777(一般需要切换到目录下进行操作,所以需要x权限)。
4,最高位代表的是特殊权限(suid:4、sgid:2、sbit:1)
5,正常情况下:管理员的umask值是0022,普通用户的umask值是0002。

那么从以上linux权限计算来看

@chmod($target, 0666 & ~umask());

就是很好的计算方式,不管是普通用户跑脚本,还是root用户

 

https://blog.csdn.net/chinaltx/article/details/86699159

https://www.cnblogs.com/songgj/p/8890710.html

posted on 2021-11-23 10:04  zh7314  阅读(725)  评论(0编辑  收藏  举报