01
<?php
02
03
// ------------ lixlpixel recursive PHP functions -------------
04
// scan_directory_recursively( directory to scan, filter )
05
// expects path to directory and optional an extension to filter
06
// of course PHP has to have the permissions to read the directory
07
// you specify and all files and folders inside this directory
08
// ------------------------------------------------------------
09
10
// to use this function to get all files and directories in an array, write:
11
// $filestructure = scan_directory_recursively('path/to/directory');
12
13
// to use this function to scan a directory and filter the results, write:
14
// $fileselection = scan_directory_recursively('directory', 'extension');
15
16
function scan_directory_recursively ( $directory , $filter = FALSE )
17
{
18
// if the path has a slash at the end we remove it here
19
if( substr ( $directory ,- 1 ) == '/' )
20
{
21
$directory = substr ( $directory , 0 ,- 1 );
22
}
23
24
// if the path is not valid or is not a directory ...
25
if(! file_exists ( $directory ) || ! is_dir ( $directory ))
26
{
27
// ... we return false and exit the function
28
return FALSE ;
29
30
// ... else if the path is readable
31
}elseif( is_readable ( $directory ))
32
{
33
// we open the directory
34
$directory_list = opendir ( $directory );
35
36
// and scan through the items inside
37
while ( FALSE !== ( $file = readdir ( $directory_list )))
38
{
39
// if the filepointer is not the current directory
40
// or the parent directory
41
if( $file != '.' && $file != '..' )
42
{
43
// we build the new path to scan
44
$path = $directory . '/' . $file ;
45
46
// if the path is readable
47
if( is_readable ( $path ))
48
{
49
// we split the new path by directories
50
$subdirectories = explode ( '/' , $path );
51
52
// if the new path is a directory
53
if( is_dir ( $path ))
54
{
55
// add the directory details to the file list
56
$directory_tree [] = array(
57
'path' => $path ,
58
'name' => end ( $subdirectories ),
59
'kind' => 'directory' ,
60
61
// we scan the new path by calling this function
62
'content' => scan_directory_recursively ( $path , $filter ));
63
64
// if the new path is a file
65
}elseif( is_file ( $path ))
66
{
67
// get the file extension by taking everything after the last dot
68
$extension = end ( explode ( '.' , end ( $subdirectories )));
69
70
// if there is no filter set or the filter is set and matches
71
if( $filter === FALSE || $filter == $extension )
72
{
73
// add the file details to the file list
74
$directory_tree [] = array(
75
'path' => $path ,
76
'name' => end ( $subdirectories ),
77
'extension' => $extension ,
78
'size' => filesize ( $path ),
79
'kind' => 'file' );
80
}
81
}
82
}
83
}
84
}
85
// close the directory
86
closedir ( $directory_list );
87
88
// return file list
89
return $directory_tree ;
90
91
// if the path is not readable ...
92
}else{
93
// ... we return false
94
return FALSE ;
95
}
96
}
97
// ------------------------------------------------------------
98
99
?>
01
<?php
02
03
// ------------ lixlpixel recursive PHP functions -------------
04
// recursive_remove_directory( directory to delete, empty )
05
// expects path to directory and optional TRUE / FALSE to empty
06
// of course PHP has to have the rights to delete the directory
07
// you specify and all files and folders inside the directory
08
// ------------------------------------------------------------
09
10
// to use this function to totally remove a directory, write:
11
// recursive_remove_directory('path/to/directory/to/delete');
12
13
// to use this function to empty a directory, write:
14
// recursive_remove_directory('path/to/full_directory',TRUE);
15
16
function recursive_remove_directory ( $directory , $empty = FALSE )
17
{
18
// if the path has a slash at the end we remove it here
19
if( substr ( $directory ,- 1 ) == '/' )
20
{
21
$directory = substr ( $directory , 0 ,- 1 );
22
}
23
24
// if the path is not valid or is not a directory ...
25
if(! file_exists ( $directory ) || ! is_dir ( $directory ))
26
{
27
// ... we return false and exit the function
28
return FALSE ;
29
30
// ... if the path is not readable
31
}elseif(! is_readable ( $directory ))
32
{
33
// ... we return false and exit the function
34
return FALSE ;
35
36
// ... else if the path is readable
37
}else{
38
39
// we open the directory
40
$handle = opendir ( $directory );
41
42
// and scan through the items inside
43
while ( FALSE !== ( $item = readdir ( $handle )))
44
{
45
// if the filepointer is not the current directory
46
// or the parent directory
47
if( $item != '.' && $item != '..' )
48
{
49
// we build the new path to delete
50
$path = $directory . '/' . $item ;
51
52
// if the new path is a directory
53
if( is_dir ( $path ))
54
{
55
// we call this function with the new path
56
recursive_remove_directory ( $path );
57
58
// if the new path is a file
59
}else{
60
// we remove the file
61
unlink ( $path );
62
}
63
}
64
}
65
// close the directory
66
closedir ( $handle );
67
68
// if the option to empty is not set to true
69
if( $empty == FALSE )
70
{
71
// try to delete the now empty directory
72
if(! rmdir ( $directory ))
73
{
74
// return false if not possible
75
return FALSE ;
76
}
77
}
78
// return success
79
return TRUE ;
80
}
81
}
82
// ------------------------------------------------------------
83
84
?>
01 <?php
02
03 // ------------ lixlpixel recursive PHP functions -------------
04 // recursive_directory_size( directory, human readable format )
05 // expects path to directory and optional TRUE / FALSE
06 // PHP has to have the rights to read the directory you specify
07 // and all files and folders inside the directory to count size
08 // if you choose to get human readable format,
09 // the function returns the filesize in bytes, KB and MB
10 // ------------------------------------------------------------
11
12 // to use this function to get the filesize in bytes, write:
13 // recursive_directory_size('path/to/directory/to/count');
14
15 // to use this function to get the size in a nice format, write:
16 // recursive_directory_size('path/to/directory/to/count',TRUE);
17
18 function recursive_directory_size($directory, $format=FALSE)
19 {
20 $size = 0;
21
22 // if the path has a slash at the end we remove it here
23 if( substr ($directory,-1) == '/')
24 {
25 $directory = substr ($directory,0,-1);
26 }
27
28 // if the path is not valid or is not a directory ...
29 if(! file_exists ($directory) || ! is_dir ($directory) || ! is_readable ($directory))
30 {
31 // ... we return -1 and exit the function
32 return -1;
33 }
34 // we open the directory
35 if($handle = opendir ($directory))
36 {
37 // and scan through the items inside
38 while(($file = readdir ($handle)) !== false)
39 {
40 // we build the new path
41 $path = $directory.'/'.$file;
42
43 // if the filepointer is not the current directory
44 // or the parent directory
45 if($file != '.' && $file != '..')
46 {
47 // if the new path is a file
48 if( is_file ($path))
49 {
50 // we add the filesize to the total size
51 $size += filesize ($path);
52
53 // if the new path is a directory
54 }elseif( is_dir ($path))
55 {
56 // we call this function with the new path
57 $handlesize = recursive_directory_size($path);
58
59 // if the function returns more than zero
60 if($handlesize >= 0)
61 {
62 // we add the result to the total size
63 $size += $handlesize;
64
65 // else we return -1 and exit the function
66 }else{
67 return -1;
68 }
69 }
70 }
71 }
72 // close the directory
73 closedir ($handle);
74 }
75 // if the format is set to human readable
76 if($format == TRUE)
77 {
78 // if the total size is bigger than 1 MB
79 if($size / 1048576 > 1)
80 {
81 return round ($size / 1048576, 1).' MB';
82
83 // if the total size is bigger than 1 KB
84 }elseif($size / 1024 > 1)
85 {
86 return round ($size / 1024, 1).' KB';
87
88 // else return the filesize in bytes
89 }else{
90 return round ($size, 1).' bytes';
91 }
92 }else{
93 // return the total filesize in bytes
94 return $size;
95 }
96 }
97 // ------------------------------------------------------------
98
99 ?>