fileperms

(PHP 3, PHP 4, PHP 5)

fileperms -- 取得文件的权限

说明

int fileperms ( string filename )

返回文件的访问权限,如果出错则返回 FALSE

注: 本函数的结果会被缓存。更多信息参见 clearstatcache()

提示: PHP 5.0.0 起本函数也可被某些 URL wrapper 使用。参考附录 L 来看哪些 wrapper 支持 stat() 系列函数的功能。

例子 1. 以八进制的形式显示文件的权限

<?php
echo substr(sprintf('%o', fileperms('/tmp')), -4);
echo
substr(sprintf('%o', fileperms('/etc/passwd')), -4);
?>

上例将输出:

1777
0644

例子 2. 输出全部权限

<?php
$perms
= fileperms('/etc/passwd');

if ((
$perms & 0xC000) == 0xC000) {
    
// Socket
    
$info = 's';
} elseif ((
$perms & 0xA000) == 0xA000) {
    
// Symbolic Link
    
$info = 'l';
} elseif ((
$perms & 0x8000) == 0x8000) {
    
// Regular
    
$info = '-';
} elseif ((
$perms & 0x6000) == 0x6000) {
    
// Block special
    
$info = 'b';
} elseif ((
$perms & 0x4000) == 0x4000) {
    
// Directory
    
$info = 'd';
} elseif ((
$perms & 0x2000) == 0x2000) {
    
// Character special
    
$info = 'c';
} elseif ((
$perms & 0x1000) == 0x1000) {
    
// FIFO pipe
    
$info = 'p';
} else {
    
// 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' : '-'));

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

echo
$info;
?>

上例将输出:

-r--r--r--

参见 is_readable()stat()


add a note add a note User Contributed Notes
ravensworld at gmx dot de
24-Oct-2006 05:58
Maybe it helps for a better reading.

function FilePermsDecode( $perms ){
       $oct = str_split( strrev( decoct( $perms ) ), 1 );
       //              0      1      2      3      4      5      6      7
       $masks = array( '---', '--x', '-w-', '-wx', 'r--', 'r-x', 'rw-', 'rwx' );
       return(
           sprintf( '%s %s %s',
                   array_key_exists( $oct[ 2 ], $masks ) ? $masks[ $oct[ 2 ] ] : '###',
                   array_key_exists( $oct[ 1 ], $masks ) ? $masks[ $oct[ 1 ] ] : '###',
                   array_key_exists( $oct[ 0 ], $masks ) ? $masks[ $oct[ 0 ] ] : '###'
                   )
             );
}
$perms = fileperms( "$u_folder/$_curId" );
if( $perms !== false ){
       printf(
               'decimal: %d | octal: %o | readable: %s',
               $perms, $perms, FilePermsDecode( $perms )
       );
}