PHP | chmod()函数文件权限用法详解

2021年3月11日18:16:40 发表评论 756 次浏览

PHP中的chmod()函数是一个内置函数, 用于将指定文件的模式更改为用户指定的特定模式。

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

语法如下:

bool chmod ( string $filename, int $mode )

使用的参数:

PHP中的chmod()函数接受两个参数, 分别是文件名和模式。

$文件名

:指定需要更改权限的文件。

$模式

:用于指定新权限。

$ mode参数由四个数值组成, 其中第一个值始终为零, 第二个值指定所有者的权限, 第三个值指定所有者的用户组的权限, 第四个值指定其他所有人的权限。

有三个可能的值, 并且要设置多个权限, 可以添加以下值。

  • 1 =执行权限
  • 2 =写入权限
  • 4 =读取权限

返回值:成功执行时返回true, 失败则返回false。

错误与异常:

  1. PHP中的chmod()函数不适用于远程文件。它仅适用于服务器文件系统可访问的文件。
  2. 如果在$ mode参数之间使用引号, 例如chmod(file.txt, " 0744"), 则PHP将隐式转换为整数数据类型。

例子:

Input : chmod("gfg.txt", 0600);
Output : true

Input : chmod("gfg.txt", 0644);
Output : true

Input : chmod("gfg.txt", 0755);
Output : true

下面的程序说明了PHP中的chmod()函数:

程序1:

<?php
  
// Read and write permission to owner
chmod ( "gfg.txt" , 0600);
  
?>

输出如下:

true

程序2:

<?php
  
// Read and write permission to owner, // and read permission to everyone else
chmod ( "gfg.txt" , 0644);
  
?>

输出如下:

true

程序3:

<?php
  
// All permissions to owner, read and
// execute permissions to everyone else
chmod ( "gfg.txt" , 0755);
  
?>

输出如下:

true

参考:

http://php.net/manual/en/function.chmod.php


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: