<?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('O arquivo informado n?o foi encontrado!');
}
$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('A informa??o requisitada n?o foi encontrada!');
}
return $this->file_info;
}
}
}
private function get_file_is_called(){
if(!$this->file){
throw new \Exception('Nenhum arquivo foi fornecido para análise. Utilize o método get_file() para isso!');
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());
?>