php 获取文件权限的方法
墨初 后端开发 3172阅读
php在操作文件时,有时候需要先获取文件的权限,用于检查文件是否可以进行修改编辑。那么今天飞鸟慕鱼就来说一下,php获取文件的权限的方法。
php fileperms() 函数介绍
fileperms():用于获取文件或文件目录的权限,用数字表示,如获取不到返回FALSE。
语法:
fileperms(filename)
参数:
filename:要获取权限的文件或文件目录
注意:fileperms() 的结果会被缓存。请使用 clearstatcache() 来清理。
php 获取文件权限的方法
1、使用 fileperms() 获取文件的权限,返回结果经数字表示
php代码:
<?php echo fileperms("test.txt"); ?>
返回结果:33206
2、PHP获取八进制的文件权限
php代码
<?php //方法1 echo substr(base_convert(fileperms("test.txt"), 10, 8), 3); //方法2 echo substr(sprintf('%o', fileperms("test.txt")), -4); ?>
返回结果:0666
3、php获取文件权限制并以“-rw-r--r--”格式返回
php代码:
<?php <?php $perms = fileperms("text.txt"); switch ($perms & 0xF000) { case 0xC000: $info = 's'; break; case 0xA000: $info = 'l'; break; case 0x8000: $info = '-'; break; case 0x6000: $info = 'b'; break; case 0x4000: $info = 'd'; break; case 0x2000: $info = 'c'; break; case 0x1000: $info = 'p'; break; default: $info = 'u'; } $info .= (($perms & 0x0100) ? 'r' : '-'); $info .= (($perms & 0x0080) ? 'w' : '-'); $info .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x') : (($perms & 0x0800) ? 'S' : '-')); $info .= (($perms & 0x0020) ? 'r' : '-'); $info .= (($perms & 0x0010) ? 'w' : '-'); $info .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x') : (($perms & 0x0400) ? 'S' : '-')); $info .= (($perms & 0x0004) ? 'r' : '-'); $info .= (($perms & 0x0002) ? 'w' : '-'); $info .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x') : (($perms & 0x0200) ? 'T' : '-')); echo $info; ?>
返回结果:-rw-rw-rw-