这是一个基于
许多用户提交的代码片段的增强版 'stat' 函数,
以及@ http://www.askapache.com/security/chmod-stat.html
传递一个文件名,它将返回一个类似于 stat 的数组。
<?php
function alt_stat($file) {
clearstatcache();
$ss=@stat($file);
if(!$ss) return false; //无法获取文件信息
$ts=array(
0140000=>'ssocket',
0120000=>'llink',
0100000=>'-file',
0060000=>'bblock',
0040000=>'ddir',
0020000=>'cchar',
0010000=>'pfifo'
);
$p=$ss['mode'];
$t=decoct($ss['mode'] & 0170000); // 文件编码位
$str =(array_key_exists(octdec($t),$ts))?$ts[octdec($t)]{0}:'u';
$str.=(($p&0x0100)?'r':'-').(($p&0x0080)?'w':'-');
$str.=(($p&0x0040)?(($p&0x0800)?'s':'x'):(($p&0x0800)?'S':'-'));
$str.=(($p&0x0020)?'r':'-').(($p&0x0010)?'w':'-');
$str.=(($p&0x0008)?(($p&0x0400)?'s':'x'):(($p&0x0400)?'S':'-'));
$str.=(($p&0x0004)?'r':'-').(($p&0x0002)?'w':'-');
$str.=(($p&0x0001)?(($p&0x0200)?'t':'x'):(($p&0x0200)?'T':'-'));
$s=array(
'perms'=>array(
'umask'=>sprintf("%04o",@umask()),
'human'=>$str,
'octal1'=>sprintf("%o", ($ss['mode'] & 000777)),
'octal2'=>sprintf("0%o", 0777 & $p),
'decimal'=>sprintf("%04o", $p),
'fileperms'=>@fileperms($file),
'mode1'=>$p,
'mode2'=>$ss['mode']),
'owner'=>array(
'fileowner'=>$ss['uid'],
'filegroup'=>$ss['gid'],
'owner'=>
(function_exists('posix_getpwuid'))?
@posix_getpwuid($ss['uid']):'',
'group'=>
(function_exists('posix_getgrgid'))?
@posix_getgrgid($ss['gid']):''
),
'file'=>array(
'filename'=>$file,
'realpath'=>(@realpath($file) != $file) ? @realpath($file) : '',
'dirname'=>@dirname($file),
'basename'=>@basename($file)
),
'filetype'=>array(
'type'=>substr($ts[octdec($t)],1),
'type_octal'=>sprintf("%07o", octdec($t)),
'is_file'=>@is_file($file),
'is_dir'=>@is_dir($file),
'is_link'=>@is_link($file),
'is_readable'=> @is_readable($file),
'is_writable'=> @is_writable($file)
),
'device'=>array(
'device'=>$ss['dev'], //设备
'device_number'=>$ss['rdev'], //设备号,如果为设备。
'inode'=>$ss['ino'], //文件序列号
'link_count'=>$ss['nlink'], //链接数
'link_to'=>($s['type']=='link') ? @readlink($file) : ''
),
'size'=>array(
'size'=>$ss['size'], //文件大小,以字节为单位。
'blocks'=>$ss['blocks'], //分配的 512 字节块数
'block_size'=> $ss['blksize'] //I/O 的最佳块大小。
),
'time'=>array(
'mtime'=>$ss['mtime'], //上次修改时间
'atime'=>$ss['atime'], //上次访问时间。
'ctime'=>$ss['ctime'], //上次状态变更时间
'accessed'=>@date('Y M D H:i:s',$ss['atime']),
'modified'=>@date('Y M D H:i:s',$ss['mtime']),
'created'=>@date('Y M D H:i:s',$ss['ctime'])
),
);
clearstatcache();
return $s;
}
?>
|=---------[ 示例输出 ]
Array(
[perms] => Array
(
[umask] => 0022
[human] => -rw-r--r--
[octal1] => 644
[octal2] => 0644
[decimal] => 100644
[fileperms] => 33188
[mode1] => 33188
[mode2] => 33188
)
[filetype] => Array
(
[type] => file
[type_octal] => 0100000
[is_file] => 1
[is_dir] =>
[is_link] =>
[is_readable] => 1
[is_writable] => 1
)
[owner] => Array
(
[fileowner] => 035483
[filegroup] => 23472
[owner_name] => askapache
[group_name] => grp22558
)
[file] => Array
(
[filename] => /home/askapache/askapache-stat/htdocs/ok/g.php
[realpath] =>
[dirname] => /home/askapache/askapache-stat/htdocs/ok
[basename] => g.php
)
[device] => Array
(
[device] => 25
[device_number] => 0
[inode] => 92455020
[link_count] => 1
[link_to] =>
)
[size] => Array
(
[size] => 2652
[blocks] => 8
[block_size] => 8192
)
[time] => Array
(
[mtime] => 1227685253
[atime] => 1227685138
[ctime] => 1227685253
[accessed] => 2008 Nov Tue 23:38:58
[modified] => 2008 Nov Tue 23:40:53
[created] => 2008 Nov Tue 23:40:53
)
)