-
Notifications
You must be signed in to change notification settings - Fork 0
/
funk.php
executable file
·62 lines (53 loc) · 2.49 KB
/
funk.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
/**
* This funtion will take a pattern and a folder as the argument and go thru it(recursivly if needed)and return the list of
* all files in that folder.
* Link : http://www.bin-co.com/php/scripts/filesystem/ls/
* Arguments : $pattern - The pattern to look out for [OPTIONAL]
* $folder - The path of the directory of which's directory list you want [OPTIONAL]
* $recursivly - The funtion will traverse the folder tree recursivly if this is true. Defaults to false. [OPTIONAL]
* $options - An array of values 'return_files' or 'return_folders' or both
* Returns : A flat list with the path of all the files(no folders) that matches the condition given.
*/
function ls($pattern="*", $folder="", $recursivly=true, $options=array('return_files')) {
if($folder){
$current_folder = realpath('.');
if(in_array('quiet', $options)){ // If quiet is on, we will suppress the 'no such folder' error
if(!file_exists($folder)) return array();
}
if(!chdir($folder)) return array();
}
$get_files = in_array('return_files', $options);
$get_folders= in_array('return_folders', $options);
$both = array();
$folders = array();
// Get the all files and folders in the given directory.
if($get_files) $both = glob($pattern, GLOB_BRACE + GLOB_MARK);
if($recursivly or $get_folders) $folders = glob("*", GLOB_ONLYDIR + GLOB_MARK);
//If a pattern is specified, make sure even the folders match that pattern.
$matching_folders = array();
if($pattern !== '*') $matching_folders = glob($pattern, GLOB_ONLYDIR + GLOB_MARK);
//Get just the files by removing the folders from the list of all files.
$all = array_values(array_diff($both,$folders));
if($recursivly or $get_folders){
foreach ($folders as $this_folder){
if($get_folders){
//If a pattern is specified, make sure even the folders match that pattern.
if($pattern !== '*'){
if(in_array($this_folder, $matching_folders)) array_push($all, $this_folder);
}
else array_push($all, $this_folder);
}
if($recursivly){
// Continue calling this function for all the folders
$deep_items = ls($pattern, $this_folder, $recursivly, $options); # :RECURSION:
foreach ($deep_items as $item){
array_push($all, $this_folder . $item);
}
}
}
}
if($folder) chdir($current_folder);
return $all;
}
?>