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.

93 lines
2.0 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
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
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. $directories = [];
  21. $files = [];
  22. // getting files from directory
  23. $items = scandir($path);
  24. foreach($items as $file) {
  25. // ignore parent directories
  26. if ($file === '.' || $file === '..') {
  27. continue;
  28. }
  29. // getting meta for file
  30. $meta = stat($path.'/'.$file);
  31. $result = [
  32. 'filename' => $file,
  33. 'is_file' => is_file($path.'/'.$file),
  34. 'meta' => [
  35. 'updated_at' => \Carbon\Carbon::parse($meta['mtime'])->format('d.m.Y h:m:i')
  36. ]
  37. ];
  38. if ($result['is_file']) {
  39. $result['size'] = $this->formatBytes($meta['size']);
  40. $files[] = $result;
  41. } else {
  42. $directories[] = $result;
  43. }
  44. }
  45. usort($directories, function($a, $b) {
  46. if (strtolower($a['filename']) < strtolower($b['filename'])) {
  47. return 0;
  48. } else {
  49. return 1;
  50. }
  51. });
  52. foreach($directories as $directory) {
  53. $results[] = $directory;
  54. }
  55. foreach($files as $file) {
  56. $results[] = $file;
  57. }
  58. return $results;
  59. }
  60. /**
  61. *
  62. * @param [type] $size [description]
  63. * @param integer $precision [description]
  64. * @return [type] [description]
  65. */
  66. private function formatBytes($size, $precision = 1)
  67. {
  68. if ($size === 0) {
  69. return '0 bytes';
  70. }
  71. $base = log($size, 1024);
  72. $suffixes = array('bytes', 'kB', 'MB', 'G', 'T');
  73. return round(pow(1024, $base - floor($base)), $precision).' '.$suffixes[floor($base)];
  74. }
  75. }