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.

72 lines
1.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. <?php
  2. namespace App;
  3. /**
  4. *
  5. *
  6. *
  7. */
  8. class FileManager
  9. {
  10. /**
  11. *
  12. *
  13. * @param [type] $path [description]
  14. * @return [type] [description]
  15. *
  16. */
  17. public function find($path)
  18. {
  19. $results = [];
  20. // getting files from directory
  21. $files = scandir($path);
  22. foreach($files as $file) {
  23. // ignore parent directories
  24. if ($file === '.' || $file === '..') {
  25. continue;
  26. }
  27. // getting meta for file
  28. $meta = stat($path.'/'.$file);
  29. $result = [
  30. 'filename' => $file,
  31. 'is_file' => is_file($path.'/'.$file),
  32. 'meta' => [
  33. 'updated_at' => \Carbon\Carbon::parse($meta['mtime'])->format('d.m.Y h:m:i')
  34. ]
  35. ];
  36. if ($result['is_file']) {
  37. $result['size'] = $this->formatBytes($meta['size']);
  38. }
  39. $results[] = $result;
  40. }
  41. return $results;
  42. }
  43. /**
  44. *
  45. * @param [type] $size [description]
  46. * @param integer $precision [description]
  47. * @return [type] [description]
  48. */
  49. private function formatBytes($size, $precision = 1)
  50. {
  51. if ($size === 0) {
  52. return '0 bytes';
  53. }
  54. $base = log($size, 1024);
  55. $suffixes = array('bytes', 'kB', 'MB', 'G', 'T');
  56. return round(pow(1024, $base - floor($base)), $precision).' '.$suffixes[floor($base)];
  57. }
  58. }