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.

69 lines
1.5 KiB

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