Php 简明教程

PHP – File Permissions

权限的概念是 Unix/Linux 文件系统核心的内容。权限决定了谁可以访问文件,以及如何访问文件。Linux 中的文件权限受 chmod command 控制,该命令可以在 Linux 终端里面运行。PHP 提供了 chmod() function ,您可以使用它以编程方式处理文件权限。

The concept of permissions is at the core of Unix/Linux file system. The permissions determine who can access a file and how one can access a file. File permissions in Linux are manipulated by the chmod command, which can be run inside the Linux terminal. PHP provides the chmod() function with which you can handle file permissions programmatically.

仅当您在 Linux 操作系统上工作时,PHP 的 chmod() 函数才是有效的。它在 Windows 上不起作用,因为 Windows操作系统的文件权限控制机制不同。

PHP’s chmod() function is effective only when you are working on a Linux OS. It doesn’t work on Windows, as Windows OS has a different mechanism of controlling file permissions.

要查看启用的文件权限,请使用 “ ls -l ” 命令获取文件列表(长列表)

To view the permissions enabled on a file, obtain the list of files using the "ls -l" command (long listing)

mvl@GNVBGL3:~$ ls -l

-rwxr-xr-x 1 mvl mvl 16376 May  5 21:52 a.out
-rw-r--r-- 1 mvl mvl    83 May  5 21:52 hello.cpp
-rwxr-xr-x 1 mvl mvl    43 Oct 11 14:50 hello.php
-rwxr-xr-x 1 mvl mvl    43 May  8 10:01 hello.py
drwxr-xr-x 5 mvl mvl  4096 Apr 20 21:52 myenv

第一列包含每个文件的权限标志。第三列和第四列指出每个文件的所有者和组,然后是大小、日期和时间以及文件名。

The first column contains permission flags of each file. Third and fourth columns indicate the owner and group of each file, followed by size, date and time, and the file name.

权限字符串有十个字符,它们的含义解释如下 −

The permissions string has ten characters, their meaning is described as follows −

Position

Meaning

1

"d" if a directory, "-" if a normal file

2, 3, 4

read, write, execute permission for user (owner) of file

5, 6, 7

read, write, execute permission for group

8, 9, 10

read, write, execute permission for other (world)

权限字符串中的字符有以下含义:

The characters in the permission string have following meaning −

Value

Meaning

-

Flag is not set.

r

File is readable.

w

File is writable. For directories, files may be created or removed.

x

File is executable. For directories, files may be listed.

如果你考虑上述列表中的第一个条目:

If you consider the first entry in the above list −

-rwxr-xr-x 1 mvl mvl 16376 May  5 21:52 a.out

“a.out”文件由用户“mvl”和组“mvl”所有。它是一个普通文件,所有者有“读/写/执行”权限,并且组和其他人有“读/执行”权限。

The "a.out" file is owned by the user "mvl" and group "mvl". It is a normal file with "read/write/execute" permissions for the owner, and "read/ execute" permissions for the group as well as others.

权限标志的二进制和八进制表示可以用下表理解:

The binary and octal representation of permission flags can be understood with the following table −

Octal Digit

Binary Representation (rwx)

Permission

0

000

none

1

001

execute only

2

010

write only

3

011

write and execute

4

100

read only

5

101

read and execute

6

110

read and write

7

111

read, write, and execute (full permissions)

The chmod() Function

chmod() 函数可以更改指定文件的权限。成功时返回 true ,否则在失败时返回 false

The chmod() function can change permissions of a specified file. It returns true on success, otherwise false on failure.

chmod(string $filename, int $permissions): bool

chmod() 函数尝试将指定文件( $filename )的模式更改为权限中给定的模式。

The chmod() function attempts to change the mode of the specified file ($filename) to that given in permissions.

第二个参数 $permissions 是一个四位八进制数字的八进制数。第一位始终为零,第二位指定所有者的权限,第三位指定所有者的用户组权限,第四位指定其他人权限。每位数字都是每种类型的权限值的和。

The second parameter $permissions is an octal number with four octal digits. The first digit is always zero, second specifies permissions for the owner, third for the owner’s user group and fourth for everybody else. Each digit is the sum of values for each type of permission.

1

Execute Permission

2

Write Permission

4

Read Permission

$permissions 参数的默认值是 0777 ,这意味着目录在启用执行、写和读权限时创建。

The default value of $permissions parameters is 0777, which means the directory is created with execute, write and read permissions enabled.

Example

请看以下示例:

Take a look at the following example −

<?php

   // Read and write for owner, nothing for everybody else
   chmod("/PhpProject/sample.txt", 0600);

   // Read and write for owner, read for everybody else
   chmod("/PhpProject/sample.txt", 0644);

   // Everything for owner, read and execute for everybody else
   chmod("/PhpProject/sample.txt", 0755);

   // Everything for owner, read for owner's group
   chmod("/PhpProject/sample.txt", 0740);
?>

The chown() Function

chown() 函数尝试将文件 filename 的所有者更改为新用户。请注意,只有超级用户可以更改文件的拥有者。

The chown() function attempts to change the owner of the file filename to a new user. Note that only the superuser may change the owner of a file.

chown(string $filename, string|int $user): bool

Example

请看以下示例:

Take a look at the following example −

<?php

   // File name and username to use
   $file_name= "index.php";
   $path = "/PhpProject/backup: " . $file_name ;
   $user_name = "root";

   // Set the user
   chown($path, $user_name);

   // Check the result
   $stat = stat($path);
   print_r(posix_getpwuid(fileowner($path)));
?>

The chgrp() Function

chgrp() 函数尝试将文件 filename 的组更改为组。

The chgrp() function attempts to change the group of the file filename to group.

chgrp(string $filename, string|int $group): bool

只有 superuser 可以随意更改文件的组;其他用户可以将文件的组更改为该用户所属的任何组。

Only a superuser may change the group of a file arbitrarily; other users may change the group of a file to any group of which that user is a member.

Example

请看以下示例:

Take a look at the following example −

<?php
   $filename = "/PhpProject/sample.txt";
   $format = "%s's Group ID @ %s: %d\n";
   printf($format, $filename, date('r'), filegroup($filename));
   chgrp($filename, "admin");
   clearstatcache();  	// do not cache filegroup() results
   printf($format, $filename, date('r'), filegroup($filename));
?>

它将生成以下 output

It will produce the following output

/PhpProject/sample.txt's Group ID @ Fri, 13 Oct 2023 07:42:21 +0200: 0
/PhpProject/sample.txt's Group ID @ Fri, 13 Oct 2023 07:42:21 +0200: 0