标签:
php的stat函数可以返回文件的各种属性,详情见:http://php.net/manual/zh/function.stat.php
其中mode字段表示文件的权限,一个int型的数值。下边的函数是将这个int型数值转换成Linux形式的格式,比如 33206 -> -rw-rw-rw-
1 /** 2 * [返回-rwxrwxrwx文件权限表达方式] 3 * @param string $filePath [文件路径] 4 * @return [string] [] 5 */ 6 function getHumanFileStat($filePath = ‘‘) 7 { 8 if(!file_exists($filePath) || !is_file($filePath)) 9 return false; 10 11 clearstatcache(); 12 $stat = stat($filePath); 13 $mode = $stat[‘mode‘]; 14 15 //参考libc‘s fstat(): http://man.he.net/man2/fstat 16 //8进制数值对应的文件类型 17 $typeMap = array( 18 0140000 => ‘ssocket‘, 19 0120000 => ‘llink‘, 20 0100000 => ‘-file‘, 21 0060000 => ‘bblock‘, 22 0040000 => ‘ddirectory‘, 23 0020000 => ‘cchar‘, 24 0010000 => ‘pfifo‘, 25 ); 26 27 //各个位的权限对应的8进制数值 28 $bitValueMap = array( 29 ‘mask‘ => 0170000, 30 ‘ubit‘ => 0004000, 31 ‘gbit‘ => 0002000, 32 ‘stickybit‘ => 0001000, 33 ‘ur‘ => 0400, 34 ‘uw‘ => 0200, 35 ‘ux‘ => 0100, 36 ‘gr‘ => 0040, 37 ‘gw‘ => 0020, 38 ‘gx‘ => 0010, 39 ‘or‘ => 0004, 40 ‘ow‘ => 0002, 41 ‘ox‘ => 0001, 42 ); 43 44 $str = ‘‘; 45 //filetype,first character 46 $str .= array_key_exists($mode & $bitValueMap[‘mask‘], $typeMap) ? substr($typeMap[$mode & $bitValueMap[‘mask‘]], 0, 1) : ‘u‘; 47 48 //user Permission 49 $str .= $mode & $bitValueMap[‘ur‘] ? ‘r‘ : ‘-‘; 50 $str .= $mode & $bitValueMap[‘uw‘] ? ‘w‘ : ‘-‘; 51 $str .= $mode & $bitValueMap[‘ux‘] ? ($mode & $bitValueMap[‘ubit‘] ? ‘s‘ : ‘x‘) : ($mode & $bitValueMap[‘ubit‘] ? ‘S‘ : ‘-‘); 52 53 //group Permission 54 $str .= $mode & $bitValueMap[‘gr‘] ? ‘r‘ : ‘-‘; 55 $str .= $mode & $bitValueMap[‘gw‘] ? ‘w‘ : ‘-‘; 56 $str .= $mode & $bitValueMap[‘gx‘] ? ($mode & $bitValueMap[‘gbit‘] ? ‘s‘ : ‘x‘) : ($mode & $bitValueMap[‘gbit‘] ? ‘S‘ : ‘-‘); 57 58 //other Permission 59 $str .= $mode & $bitValueMap[‘or‘] ? ‘r‘ : ‘-‘; 60 $str .= $mode & $bitValueMap[‘ow‘] ? ‘w‘ : ‘-‘; 61 $str .= $mode & $bitValueMap[‘ox‘] ? ($mode & $bitValueMap[‘stickybit‘] ? ‘t‘ : ‘x‘) : ($mode & $bitValueMap[‘stickybit‘] ? ‘T‘ : ‘-‘); 62 63 clearstatcache(); 64 return $str; 65 }
参考文献:
http://php.net/manual/zh/function.stat.php
http://technosophos.com/2012/02/15/php-mode-strings-stat-and-stream-wrappers.html
http://man.he.net/man2/fstat
标签:
原文地址:http://www.cnblogs.com/cxybjfu/p/4768838.html