chmod

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

chmod改变文件模式

说明

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

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

参数

filename

文件的路径。

mode

注意 mode 不会被自动当成八进制数值,而且也不能用字符串(例如 "g+w")。要确保正确操作,需要给 mode 前面加上 0:

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

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

<?php
// Read and write for owner, nothing for everybody else
chmod("/somedir/somefile"0600);

// Read and write for owner, read for everybody else
chmod("/somedir/somefile"0644);

// Everything for owner, read and execute for others
chmod("/somedir/somefile"0755);

// Everything for owner, read and execute for owner's group
chmod("/somedir/somefile"0750);
?>

返回值

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

注释

注意:

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

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

参见