PHP obtains the permission information of the file (obtain permission information, return string meaning, binary conversion method, permission modification)

illustrate

insert image description here
(The picture comes from the Internet)

File permissions refer to the access permissions of files or directories to users and other processes. In Unix and Linux systems, files and directories have three permissions: read, write, and execute. These three permissions are denoted by the numbers 1, 2, and 4, respectively.
For example, the file file.txt might have the following permissions:

-rwxr-xr-x 1 user group other

Among them, rwx indicates that the user can read, write and execute the file; rx indicates that the user can read and execute the file; r-- indicates that the user can only read the file; -x indicates that the user cannot execute the file.
File permissions are usually expressed in octal numbers, such as 644, which means that the file has read, write, and execute permissions for users and other processes. On Windows systems, file permissions are usually expressed as hexadecimal numbers, such as 0644.
In permission setting, the following aspects usually need to be considered:
User: Who can access the file or directory?
Groups: Which user groups can access the file or directory?
Miscellaneous: How do I control access to files or directories by other processes?
By setting file permissions, you can protect the security of files and directories, and you can also control how files are used.

1. Obtain the permission information of the file

fileperms()Function can be used in PHP to get file permission information. This function can receive a file name or a file handle as a parameter, and returns an octal value representing the current file permissions. You can use the octal number to compare the corresponding permission flags to determine the read and write permissions of the file. Here is a sample code:

function getFilePermission($filename) {
    
    
    clearstatcache(true, $filename);
    $perms = fileperms($filename);
    if (($perms & 0xC000) === 0xC000) {
    
    
        $info = 's';
    } elseif (($perms & 0xA000) === 0xA000) {
    
    
        $info = 'l';
    } elseif (($perms & 0x8000) === 0x8000) {
    
    
        $info = '-';
    } elseif (($perms & 0x6000) === 0x6000) {
    
    
        $info = 'b';
    } elseif (($perms & 0x4000) === 0x4000) {
    
    
        $info = 'd';
    } elseif (($perms & 0x2000) === 0x2000) {
    
    
        $info = 'c';
    } elseif (($perms & 0x1000) === 0x1000) {
    
    
        $info = 'p';
    } else {
    
    
        $info = 'u';
    }

    $info .= (($perms & 0x0100) ? 'r' : '-');
    $info .= (($perms & 0x0080) ? 'w' : '-');
    $info .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-'));
    $info .= (($perms & 0x0020) ? 'r' : '-');
    $info .= (($perms & 0x0010) ? 'w' : '-');
    $info .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-'));
    $info .= (($perms & 0x0004) ? 'r' : '-');
    $info .= (($perms & 0x0002) ? 'w' : '-');
    $info .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-'));

    return $info;
}

// 使用示例
echo getFilePermission('example.txt'); // 返回 "-rw-rw-r--"

This function will return a string representing the file permission according to the file permission setting. Among them, the first character represents the file type, and the next nine characters represent the read and write permissions of the file owner, group and others, 'r' means read-only permission, 'w' means writable permission, 'x' means Executable permission, '-' means no permission, and the combination of these characters can be used to represent all permission information of the file.

2. Return the interpretation of file permission characters

Return result: -rwxr-xr-x

-rwxr-xr-x, in the Linux system, permissions are distinguished by users, that is, users, group users, and other users. The first digit indicates the type of the file, - represents the file, d represents the directory, and each other user occupies three characters , where -rwxr-xr-x corresponds to the following relationship

The first one user group user other users
- rwx r-x r-x

This is the permission representation of a file or directory, consisting of 10 characters in total, expressed from left to right:

  • The first character indicates the file type (- indicates a normal file, d indicates a directory, l indicates a symbolic link file, etc.);
  • The next three characters indicate the permissions of the file owner, r means read permissions, w means write permissions, x means executable permissions; if there is no permission, use - to indicate;
  • The next three characters indicate the permissions of the file owner's group, and the format is the same as before;
  • The last three characters represent the permissions of other users (that is, users who are not the owner of the file or the group they belong to), and the format is consistent with the previous one.

Therefore, -rwxr-xr-x means that the owner of the file has read, write, and execute permissions, and the group and other users have read and execute permissions. This is a very common permission setting, which means that the owner of this file has full control over it, while other users can only read and execute it.

3. Convert to binary permissions

In the Linux system, file permissions are represented by three octal numbers, corresponding to file owner permissions, group permissions, and other user permissions. Each octal number is composed of three bits, and there are eight bits in total, and each bit can be 0 or 1, indicating the respective permissions.

Among them, r means read permission, w means write permission, x means execute permission, and the corresponding binary numbers are: r=100, w=010, x=001.

Therefore, the three octal numbers corresponding to -rwxr-xr-x are: 755, and the permissions corresponding to each bit are as follows:

File owner: 7 (binary 111)

  • rwx (i.e. 4+2+1 = 7)

In group: 5 (binary 101)

  • rx (i.e. 4+0+1 = 5)

Other users: 5 (binary 101)

  • rx (i.e. 4+0+1 = 5)

Among them, the number 7 means that all permissions are enabled, that is, rwx; the number 5 means that the read and execute permissions are enabled, but the write permission is turned off, that is, rx.

4. Modify permissions

function chmod($file, $permissions) {
    
    
    if (file_exists($file)) {
    
    
        $permissions = octdec($permissions);
        chmod($file, $permissions);
        return true;
    } else {
    
    
        return false;
    }
}

This function accepts two parameters: the filename and the permissions to modify. First, it checks to see if the file exists, if it does, converts the permissions to decimal numbers using the octdec() function, and then modifies the permissions using the chmod() function. Returns false if the file does not exist.

$file = '/path/to/file.txt';
$permissions = 0644; // 将权限设置为 640
if (chmod($file, $permissions)) {
    
    
    echo '文件权限已修改。';
} else {
    
    
    echo '文件权限修改失败。';
}

Note: 如果文件不存在,则无法使用 chmod() 函数修改其权限.


@leak time sometimes

Guess you like

Origin blog.csdn.net/weixin_41290949/article/details/130931572