You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

94 lines
2.0 KiB

<?php
namespace App;
/**
*
*
*
*/
class FileManager
{
/**
*
*
* @param [type] $path [description]
* @return [type] [description]
*
*/
public function find($path)
{
$results = [];
$directories = [];
$files = [];
// getting files from directory
$items = scandir($path);
foreach($items as $file) {
// ignore parent directories
if ($file === '.' || $file === '..') {
continue;
}
// getting meta for file
$meta = stat($path.'/'.$file);
$result = [
'filename' => $file,
'is_file' => is_file($path.'/'.$file),
'meta' => [
'updated_at' => \Carbon\Carbon::parse($meta['mtime'])->format('d.m.Y h:m:i')
]
];
if ($result['is_file']) {
$result['size'] = $this->formatBytes($meta['size']);
$files[] = $result;
} else {
$directories[] = $result;
}
}
usort($directories, function($a, $b) {
if (strtolower($a['filename']) < strtolower($b['filename'])) {
return 0;
} else {
return 1;
}
});
foreach($directories as $directory) {
$results[] = $directory;
}
foreach($files as $file) {
$results[] = $file;
}
return $results;
}
/**
*
* @param [type] $size [description]
* @param integer $precision [description]
* @return [type] [description]
*/
private function formatBytes($size, $precision = 1)
{
if ($size === 0) {
return '0 bytes';
}
$base = log($size, 1024);
$suffixes = array('bytes', 'kB', 'MB', 'G', 'T');
return round(pow(1024, $base - floor($base)), $precision).' '.$suffixes[floor($base)];
}
}