php技术
当前位置:首页 > 帮助中心 > php技术 > 正文内容

php技术

php获取文件权限

豫唐网络2023-03-07 08:0358

获取文件的权限,权限格式分为数值格式(如0644)和字符表达格式(如-rw-r--r--)两种

数值格式应用案例

$url="1.txt";
$name=GetFilePermsOct($url);
print_r($name);

输出结果为:

0644

数值格式函数

function GetFilePermsOct($f)
{
    if (!file_exists($f)) {
        return '';
    }

    return substr(sprintf('%o', fileperms($f)), -4);
}

字符表达格式应用案例

$url="1.txt";
$name=GetFilePerms($url);
print_r($name);

输出结果为:

-rw-r--r--

字符表达格式函数

function GetFilePerms($f)
{
    if (!file_exists($f)) {
        return '';
    }

    $perms = fileperms($f);
    switch ($perms & 0xF000) {
        case 0xC000: // socket
            $info = 's';
            break;
        case 0xA000: // symbolic link
            $info = 'l';
            break;
        case 0x8000: // regular
            $info = '-';
            break;
        case 0x6000: // block special
            $info = 'b';
            break;
        case 0x4000: // directory
            $info = 'd';
            break;
        case 0x2000: // character special
            $info = 'c';
            break;
        case 0x1000: // FIFO pipe
            $info = 'p';
            break;
        default: // unknown
            $info = 'u';
    }

    // Owner
    $info .= (($perms & 0x0100) ? 'r' : '-');
    $info .= (($perms & 0x0080) ? 'w' : '-');
    $info .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x') : (($perms & 0x0800) ? 'S' : '-'));

    // Group
    $info .= (($perms & 0x0020) ? 'r' : '-');
    $info .= (($perms & 0x0010) ? 'w' : '-');
    $info .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x') : (($perms & 0x0400) ? 'S' : '-'));

    // Other
    $info .= (($perms & 0x0004) ? 'r' : '-');
    $info .= (($perms & 0x0002) ? 'w' : '-');
    $info .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x') : (($perms & 0x0200) ? 'T' : '-'));

    return $info;
}

扫描二维码推送至手机访问。

版权声明:本文由汤阴县豫唐网络科技有限公司发布,如需转载请注明出处。

本文链接:https://www.ytecn.com/post/566.html

分享给朋友:

相关文章

php将编码转换为UTF8

php将编码转换为UTF8

主要用于编码不统一导致出现乱码的情况,此函数会自动监测非UTF8编码转成UTF8编码。function ConverCode($str){ $encode = mb_d...

vscode运行php和Composer

vscode运行php和Composer

需要用到的工具1、安装php(官网下载)2、安装composer(官网)3、vscode插件PHP Server4、vscode插件PHP Debug5、windows 11系统步骤1、安装php安装...

php获取网站在服务器中用的环境

php获取网站在服务器中用的环境

判断当前网站在服务器用的什么环境function GetWebServer() {     if (!isset($_SERVER[&#...

phpRSA加密解密函数

phpRSA加密解密函数

使用方法:加密 $txt="ytecn";  $pubkey="公钥"  $macdata = RSAEn...

php远程提交post函数

php远程提交post函数

远程提交方式:post范围:所有php类型程序函数代码function post($params, $url) {     $c...

php分割string并取某项数据

php分割string并取某项数据

对string进行分割,并取某项数据。应用案例$txt="姓名|电话|手机号|豫唐"; $name=SplitAndGet($txt,"|",3); ech...