<?php
class FileInfoTool {
private $file;
private $file_info;
public function get_file(string $file){
clearstatcache();
$file = str_replace(array('/', '\\'), array(DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR), $file);
if(!is_file($file) && !is_executable($file) && !is_readable($file)){
throw new \Exception('未找到指定的文件!');
}
$this->file = $file;
$this->set_file_info($this->file);
return $this;
}
public function get_info($index = ''){
if($this->get_file_is_called()){
if($index === ''){
return $this->file_info;
}
if($index != ''){
if(!array_key_exists($index, $this->file_info)){
throw new \Exception('未找到请求的信息!');
}
return $this->file_info;
}
}
}
private function get_file_is_called(){
if(!$this->file){
throw new \Exception('未提供任何文件进行分析。 请使用 get_file() 方法。');
return false;
}
return true;
}
private function set_file_info(){
$this->file_info = array();
$pathinfo = pathinfo($this->file);
$stat = stat($this->file);
$this->file_info['realpath'] = realpath($this->file);
$this->file_info['dirname'] = $pathinfo['dirname'];
$this->file_info['basename'] = $pathinfo['basename'];
$this->file_info['filename'] = $pathinfo['filename'];
$this->file_info['extension'] = $pathinfo['extension'];
$this->file_info['mime'] = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $this->file);
$this->file_info['encoding'] = finfo_file(finfo_open(FILEINFO_MIME_ENCODING), $this->file);
$this->file_info['size'] = $stat[7];
$this->file_info['size_string'] = $this->format_bytes($stat[7]);
$this->file_info['atime'] = $stat[8];
$this->file_info['mtime'] = $stat[9];
$this->file_info['permission'] = substr(sprintf('%o', fileperms($this->file)), -4);
$this->file_info['fileowner'] = getenv('USERNAME');
}
private function format_bytes(int $size){
$base = log($size, 1024);
$suffixes = array('', 'KB', 'MB', 'GB', 'TB');
return round(pow(1024, $base-floor($base)), 2).''.$suffixes[floor($base)];
}
}
var_dump((new FileInfoTool)->get_file('sitemap.xml')->get_info());
?>