PHP | chmod()函数

PHP中的chmod()函数是一个内置函数,用于将指定文件的模式更改为用户给定的特定模式。 函数的作用是:更改指定文件的权限,成功时返回true,失败时返回false。

null

语法:

bool chmod ( string $filename, int $mode )

使用的参数: PHP中的chmod()函数接受两个参数,即filename和mode。

  1. $filename :指定需要更改其权限的文件。
  2. $mode :用于指定新权限。 $mode参数由四个数值组成,其中第一个值始终为零,第二个值指定所有者的权限,第三个值指定所有者用户组的权限,第四个值指定其他所有人的权限。 有三个可能的值,要设置多个权限,可以添加以下值。
    • 1=执行权限
    • 2=写入权限
    • 4=读取权限

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

错误和例外 :

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

例如:

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

下面的程序演示了PHP中的chmod()函数:

方案1 :

PHP

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


输出:

true

方案2 :

PHP

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


输出:

true

方案3 :

PHP

<?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

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享