Filesystem 函数
PHP 手册

chmod

(PHP 4, PHP 5)

chmod改变文件模式

说明

bool chmod ( string $filename , int $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

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

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

Note: 安全模式打开的时候,PHP 会检查所操作的文件是否和正在执行的脚本具有相同的 UID (所有者)。要注意的是,不能修改 SUID,SGID 和 sticky bits。

参见 chown()chgrp()


Filesystem 函数
PHP 手册