1
+ <?php
2
+ namespace TriHydera \File ;
3
+
4
+ class FileInfo {
5
+ private $ dir ;
6
+
7
+ public function __construct ($ dir ) {
8
+ $ this ->dir = $ dir ;
9
+ }
10
+
11
+ public function getInfo () {
12
+ $ details = [];
13
+ $ this ->listFilesRecursively ($ this ->dir , $ details );
14
+ return $ details ;
15
+ }
16
+
17
+ private function listFilesRecursively ($ dir , &$ details ) {
18
+ $ files = scandir ($ dir );
19
+
20
+ foreach ($ files as $ file ) {
21
+ if ($ file === '. ' || $ file === '.. ' ) {
22
+ continue ;
23
+ }
24
+
25
+ $ path = $ dir . '/ ' . $ file ;
26
+
27
+ if (is_dir ($ path )) {
28
+ $ this ->listFilesRecursively ($ path , $ details );
29
+ } else {
30
+ $ hash = md5_file ($ path );
31
+ $ size = filesize ($ path );
32
+ $ modified = filemtime ($ path );
33
+ $ formattedSize = $ this ->formatSize ($ size );
34
+ $ shortCode = substr ($ hash , 2 , 4 ).substr ($ hash , 10 , 4 );
35
+
36
+ $ details [$ file ] = [
37
+ 'id ' => $ shortCode ,
38
+ 'name ' => $ file ,
39
+ 'modified ' => date ('Y-m-d H:i ' , $ modified ),
40
+ 'size_raw ' => $ size ,
41
+ 'size ' => $ formattedSize ,
42
+ 'hash ' => $ hash ,
43
+ 'path ' => $ path
44
+ ];
45
+ }
46
+ }
47
+ }
48
+
49
+ private function formatSize ($ bytes ) {
50
+ $ units = array ('B ' , 'KB ' , 'MB ' , 'GB ' , 'TB ' );
51
+ $ i = floor (log ($ bytes , 1024 ));
52
+ return @round ($ bytes / pow (1024 , $ i ), 2 ) . ' ' . $ units [$ i ];
53
+ }
54
+ }
55
+ ?>
0 commit comments