11<?php
2+
23/**
34 * CodeIgniter
45 *
3536 * @since Version 1.0.0
3637 * @filesource
3738 */
38-
3939/**
4040 * CodeIgniter Directory Helpers
4141 *
6666 */
6767 function directory_map (string $ source_dir , int $ directory_depth = 0 , bool $ hidden = false ): array
6868 {
69- if ( $ fp = @ opendir ( $ source_dir ))
69+ try
7070 {
71+ $ fp = opendir ($ source_dir );
72+
7173 $ filedata = [];
7274 $ new_depth = $ directory_depth - 1 ;
7375 $ source_dir = rtrim ($ source_dir , DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR ;
@@ -95,8 +97,10 @@ function directory_map(string $source_dir, int $directory_depth = 0, bool $hidde
9597 closedir ($ fp );
9698 return $ filedata ;
9799 }
98-
99- return [];
100+ catch (\Exception $ fe )
101+ {
102+ return [];
103+ }
100104 }
101105
102106}
@@ -120,25 +124,29 @@ function directory_map(string $source_dir, int $directory_depth = 0, bool $hidde
120124 */
121125 function write_file (string $ path , string $ data , string $ mode = 'wb ' ): bool
122126 {
123- if ( ! $ fp = @ fopen ( $ path , $ mode ))
127+ try
124128 {
125- return false ;
126- }
129+ $ fp = fopen ($ path , $ mode );
127130
128- flock ($ fp , LOCK_EX );
131+ flock ($ fp , LOCK_EX );
129132
130- for ($ result = $ written = 0 , $ length = strlen ($ data ); $ written < $ length ; $ written += $ result )
131- {
132- if (($ result = fwrite ($ fp , substr ($ data , $ written ))) === false )
133+ for ($ result = $ written = 0 , $ length = strlen ($ data ); $ written < $ length ; $ written += $ result )
133134 {
134- break ;
135+ if (($ result = fwrite ($ fp , substr ($ data , $ written ))) === false )
136+ {
137+ break ;
138+ }
135139 }
136- }
137140
138- flock ($ fp , LOCK_UN );
139- fclose ($ fp );
141+ flock ($ fp , LOCK_UN );
142+ fclose ($ fp );
140143
141- return is_int ($ result );
144+ return is_int ($ result );
145+ }
146+ catch (\Exception $ fe )
147+ {
148+ return false ;
149+ }
142150 }
143151
144152}
@@ -168,29 +176,33 @@ function delete_files(string $path, bool $delDir = false, bool $htdocs = false,
168176 // Trim the trailing slash
169177 $ path = rtrim ($ path , '/ \\' );
170178
171- if ( ! $ current_dir = @ opendir ( $ path ))
179+ try
172180 {
173- return false ;
174- }
181+ $ current_dir = opendir ($ path );
175182
176- while (false !== ($ filename = @readdir ($ current_dir )))
177- {
178- if ($ filename !== '. ' && $ filename !== '.. ' )
183+ while (false !== ($ filename = @readdir ($ current_dir )))
179184 {
180- if (is_dir ($ path . DIRECTORY_SEPARATOR . $ filename ) && $ filename [0 ] !== '. ' )
181- {
182- delete_files ($ path . DIRECTORY_SEPARATOR . $ filename , $ delDir , $ htdocs , $ _level + 1 );
183- }
184- elseif ($ htdocs !== true || ! preg_match ('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i ' , $ filename ))
185+ if ($ filename !== '. ' && $ filename !== '.. ' )
185186 {
186- @unlink ($ path . DIRECTORY_SEPARATOR . $ filename );
187+ if (is_dir ($ path . DIRECTORY_SEPARATOR . $ filename ) && $ filename [0 ] !== '. ' )
188+ {
189+ delete_files ($ path . DIRECTORY_SEPARATOR . $ filename , $ delDir , $ htdocs , $ _level + 1 );
190+ }
191+ elseif ($ htdocs !== true || ! preg_match ('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i ' , $ filename ))
192+ {
193+ @unlink ($ path . DIRECTORY_SEPARATOR . $ filename );
194+ }
187195 }
188196 }
189- }
190197
191- closedir ($ current_dir );
198+ closedir ($ current_dir );
192199
193- return ($ delDir === true && $ _level > 0 ) ? @rmdir ($ path ) : true ;
200+ return ($ delDir === true && $ _level > 0 ) ? @rmdir ($ path ) : true ;
201+ }
202+ catch (\Exception $ fe )
203+ {
204+ return false ;
205+ }
194206 }
195207
196208}
@@ -216,8 +228,9 @@ function get_filenames(string $source_dir, bool $include_path = false, bool $rec
216228 {
217229 static $ filedata = [];
218230
219- if ( $ fp = @ opendir ( $ source_dir ))
231+ try
220232 {
233+ $ fp = opendir ($ source_dir );
221234 // reset the array and make sure $source_dir has a trailing slash on the initial call
222235 if ($ recursion === false )
223236 {
@@ -240,8 +253,10 @@ function get_filenames(string $source_dir, bool $include_path = false, bool $rec
240253 closedir ($ fp );
241254 return $ filedata ;
242255 }
243-
244- return [];
256+ catch (\Exception $ fe )
257+ {
258+ return [];
259+ }
245260 }
246261
247262}
@@ -270,34 +285,38 @@ function get_dir_file_info(string $source_dir, bool $top_level_only = true, bool
270285 static $ filedata = [];
271286 $ relative_path = $ source_dir ;
272287
273- if ( $ fp = @ opendir ( $ source_dir ))
288+ try
274289 {
275- // reset the array and make sure $source_dir has a trailing slash on the initial call
276- if ($ recursion === false )
277- {
278- $ filedata = [];
279- $ source_dir = rtrim (realpath ($ source_dir ), DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR ;
280- }
281-
282- // Used to be foreach (scandir($source_dir, 1) as $file), but scandir() is simply not as fast
283- while (false !== ($ file = readdir ($ fp )))
284- {
285- if (is_dir ($ source_dir . $ file ) && $ file [0 ] !== '. ' && $ top_level_only === false )
290+ $ fp = @opendir ($ source_dir ); {
291+ // reset the array and make sure $source_dir has a trailing slash on the initial call
292+ if ($ recursion === false )
286293 {
287- get_dir_file_info ($ source_dir . $ file . DIRECTORY_SEPARATOR , $ top_level_only , true );
294+ $ filedata = [];
295+ $ source_dir = rtrim (realpath ($ source_dir ), DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR ;
288296 }
289- elseif ($ file [0 ] !== '. ' )
297+
298+ // Used to be foreach (scandir($source_dir, 1) as $file), but scandir() is simply not as fast
299+ while (false !== ($ file = readdir ($ fp )))
290300 {
291- $ filedata [$ file ] = get_file_info ($ source_dir . $ file );
292- $ filedata [$ file ]['relative_path ' ] = $ relative_path ;
301+ if (is_dir ($ source_dir . $ file ) && $ file [0 ] !== '. ' && $ top_level_only === false )
302+ {
303+ get_dir_file_info ($ source_dir . $ file . DIRECTORY_SEPARATOR , $ top_level_only , true );
304+ }
305+ elseif ($ file [0 ] !== '. ' )
306+ {
307+ $ filedata [$ file ] = get_file_info ($ source_dir . $ file );
308+ $ filedata [$ file ]['relative_path ' ] = $ relative_path ;
309+ }
293310 }
294- }
295311
296- closedir ($ fp );
297- return $ filedata ;
312+ closedir ($ fp );
313+ return $ filedata ;
314+ }
315+ }
316+ catch (\Exception $ fe )
317+ {
318+ return [];
298319 }
299-
300- return [];
301320 }
302321
303322}
@@ -318,9 +337,9 @@ function get_dir_file_info(string $source_dir, bool $top_level_only = true, bool
318337 * @param string $file Path to file
319338 * @param mixed $returned_values Array or comma separated string of information returned
320339 *
321- * @return array
340+ * @return array|null
322341 */
323- function get_file_info (string $ file , $ returned_values = ['name ' , 'server_path ' , 'size ' , 'date ' ]): array
342+ function get_file_info (string $ file , $ returned_values = ['name ' , 'server_path ' , 'size ' , 'date ' ])
324343 {
325344 if ( ! file_exists ($ file ))
326345 {
@@ -334,8 +353,7 @@ function get_file_info(string $file, $returned_values = ['name', 'server_path',
334353
335354 foreach ($ returned_values as $ key )
336355 {
337- switch ($ key )
338- {
356+ switch ($ key ) {
339357 case 'name ' :
340358 $ fileinfo ['name ' ] = basename ($ file );
341359 break ;
0 commit comments