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.

104 lines
2.2 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
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['meta']['size'] = $this->formatBytes($meta['size']);
  40. $files[] = $result;
  41. } else {
  42. $directories[] = $result;
  43. }
  44. }
  45. // sorting by filename
  46. usort($directories, [$this, 'sortByFilename']);
  47. usort($files, [$this, 'sortByFilename']);
  48. // adding directories
  49. foreach($directories as $directory) {
  50. $results[] = $directory;
  51. }
  52. // adding files
  53. foreach($files as $file) {
  54. $results[] = $file;
  55. }
  56. return $results;
  57. }
  58. /**
  59. * sorting by filename, function for usort
  60. *
  61. */
  62. private function sortByFilename($a, $b)
  63. {
  64. if (strtolower($a['filename']) < strtolower($b['filename'])) {
  65. return 0;
  66. } else {
  67. return 1;
  68. }
  69. }
  70. /**
  71. *
  72. * @param [type] $size [description]
  73. * @param integer $precision [description]
  74. * @return [type] [description]
  75. */
  76. private function formatBytes($size, $precision = 1)
  77. {
  78. if ($size === 0) {
  79. return '0 bytes';
  80. }
  81. $base = log($size, 1024);
  82. $suffixes = array('bytes', 'kB', 'MB', 'G', 'T');
  83. return round(pow(1024, $base - floor($base)), $precision).' '.$suffixes[floor($base)];
  84. }
  85. }