|
|
- <?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['meta']['size'] = $this->formatBytes($meta['size']);
- $files[] = $result;
- } else {
- $directories[] = $result;
- }
- }
-
- // sorting by filename
- usort($directories, [$this, 'sortByFilename']);
- usort($files, [$this, 'sortByFilename']);
-
- // adding directories
- foreach($directories as $directory) {
- $results[] = $directory;
- }
-
- // adding files
- foreach($files as $file) {
- $results[] = $file;
- }
-
- return $results;
- }
-
- /**
- * sorting by filename, function for usort
- *
- */
- private function sortByFilename($a, $b)
- {
- if (strtolower($a['filename']) < strtolower($b['filename'])) {
- return 0;
- } else {
- return 1;
- }
- }
-
- /**
- *
- * @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)];
- }
-
- }
|