chmod

(PHP 4, PHP 5, PHP 7, PHP 8)

chmod改变文件模式

说明

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

尝试将 filename 所指定文件的模式改成 permissions 所给定的。

参数

filename

文件的路径。

permissions

注意 permissions 不会被自动当成八进制数值,因此为确保正确操作,需要给 permissions 前面加上 0。诸如“g+w”之类的字符串将无法正常工作。

<?php
chmod
("/somedir/somefile", 755); // 十进制数,可能不对
chmod("/somedir/somefile", "u+rwx,go+rx"); // 字符串,不对
chmod("/somedir/somefile", 0755); // 八进制数,正确的 mode 值
?>

permissions 参数包含三个八进制数按顺序分别指定了所有者、所有者所在的组以及所有人的访问限制。每一部分都可以通过加入所需的权限来计算出所要的权限。数字 1 表示使文件可执行,数字 2 表示使文件可写,数字 4 表示使文件可读。加入这些数字来制定所需要的权限。有关 UNIX 系统的文件权限可以阅读手册“man 1 chmod”和“man 2 chmod”。

<?php
// 所有者可以读写,其他人什么也不能做
chmod("/somedir/somefile", 0600);

// 所有者可以读写,其他人可以读
chmod("/somedir/somefile", 0644);

// 所有者可以读写执行,其他人可以读和执行
chmod("/somedir/somefile", 0755);

// 所有者可以读写执行,所有者同组可以读和执行
chmod("/somedir/somefile", 0750);
?>

返回值

成功时返回 true, 或者在失败时返回 false

错误/异常

失败时会发出 E_WARNING

注释

注意:

当前用户指的是执行 PHP 的用户。很可能和通常访问 shell 或者 FTP 的用户不是同一个。在大多数系统下文件模式只能被拥有该文件的用户改变。

注意: 此函数不能作用于远程文件,被检查的文件必须是可通过服务器的文件系统访问的。

参见

添加备注

用户贡献的备注 6 notes

up
58
MethodicalFool
14 years ago
BEWARE, a couple of the examples in the comments suggest doing something like this:

chmod(file_or_dir_name, intval($mode, 8));

However, if $mode is an integer then intval( ) won't modify it. So, this code...

$mode = 644;
chmod('/tmp/test', intval($mode, 8));

...produces permissions that look like this:

1--w----r-T

Instead, use octdec( ), like this:

chmod(file_or_dir_name, octdec($mode));

See also: http://www.php.net/manual/en/function.octdec.php
up
38
Geoff W
14 years ago
BEWARE using quotes around the second parameter...

If you use quotes eg

chmod (file, "0644");

php will not complain but will do an implicit conversion to an int before running chmod. Unfortunately the implicit conversion doesn't take into account the octal string so you end up with an integer version 644, which is 1204 octal
up
36
masha at mail dot ru
19 years ago
Usefull reference:

Value Permission Level
400 Owner Read
200 Owner Write
100 Owner Execute
40 Group Read
20 Group Write
10 Group Execute
4 Global Read
2 Global Write
1 Global Execute

(taken from http://www.onlamp.com/pub/a/php/2003/02/06/php_foundations.html)
up
8
chris at ocproducts dot com
4 years ago
Windows has a very different file permission model to Unix and integrates them only minimally.

On Windows, all this function can do is to change the "read only" flag, which is turned on if $mode & 0200 does not pass.
i.e. it only checks if u+w is missing from the bitmask, and if it is, it sets the read only flag.

The executable flag cannot be set as Windows determines it based on file extension.
The write flag cannot be set as Windows determines write access based on ACLs, which are not integrated here.
up
6
alex at feidesign dot com
19 years ago
If you cannot chmod files/directories with PHP because of safe_mode restrictions, but you can use FTP to chmod them, simply use PHP's FTP-functions (eg. ftp_chmod or ftp_site) instead. Not as efficient, but works.
up
2
sander
15 years ago
if you want to chmod directories too, use this

<?php
$iterator
= new RecursiveIteratorIterator(new RecursiveDirectoryIterator($pathname), RecursiveIteratorIterator::SELF_FIRST);

foreach(
$iterator as $item) {
chmod($item, $filemode);
}
?>
To Top