useful recursive PHP functions

recursively ...

Recursive Directory Scan Function

Recursively scan directory and list all files with this function.

This PHP function scans a given directory and lists all files and subdirectories it finds and has permission to read.
The script recursively goes through all directories and retains the directory/content relation.

It returns an array with the complete file structure and many file informations like filesize, path, name and extension.
You can very easily add whatever PHP function you want, like last modified time, mime-type and more.

Start playing around with this and you will enjoy the

scan directory recursively function:

download Recursive Directory Scan Function with comments

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         // initialize directory tree variable
34         $directory_tree = array();
35  
36         // we open the directory
37         $directory_list = opendir($directory);
38  
39         // and scan through the items inside
40         while (FALSE !== ($file = readdir($directory_list)))
41         {
42             // if the filepointer is not the current directory
43             // or the parent directory
44             if($file != '.' && $file != '..')
45             {
46                 // we build the new path to scan
47                 $path = $directory.'/'.$file;
48  
49                 // if the path is readable
50                 if(is_readable($path))
51                 {
52                     // we split the new path by directories
53                     $subdirectories = explode('/',$path);
54  
55                     // if the new path is a directory
56                     if(is_dir($path))
57                     {
58                         // add the directory details to the file list
59                         $directory_tree[] = array(
60                             'path'    => $path,
61                             'name'    => end($subdirectories),
62                             'kind'    => 'directory',
63  
64                             // we scan the new path by calling this function
65                             'content' => scan_directory_recursively($path, $filter));
66  
67                     // if the new path is a file
68                     }elseif(is_file($path))
69                     {
70                         // get the file extension by taking everything after the last dot
71                         $extension = end(explode('.',end($subdirectories)));
72  
73                         // if there is no filter set or the filter is set and matches
74                         if($filter === FALSE || $filter == $extension)
75                         {
76                             // add the file details to the file list
77                             $directory_tree[] = array(
78                                 'path'      => $path,
79                                 'name'      => end($subdirectories),
80                                 'extension' => $extension,
81                                 'size'      => filesize($path),
82                                 'kind'      => 'file');
83                         }
84                     }
85                 }
86             }
87         }
88         // close the directory
89         closedir($directory_list);
90  
91         // return file list
92         return $directory_tree;
93  
94     // if the path is not readable ...
95     }else{
96         // ... we return false
97         return FALSE;
98     }
99 }
100 // ------------------------------------------------------------
101
102 ?>

learn more about the PHP functions used in this script:

substr( ), file_exists( ), is_dir( ), is_readable( ), opendir( ), readdir( ), explode( ), end( ), is_file( ), filesize( ) and closedir( ).

recursive directory scan function without comments:

download Recursive Directory Scan Function

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 // ------------------------------------------------------------
07 function scan_directory_recursively($directory, $filter=FALSE)
08 {
09     if(substr($directory,-1) == '/')
10     {
11         $directory = substr($directory,0,-1);
12     }
13     if(!file_exists($directory) || !is_dir($directory))
14     {
15         return FALSE;
16     }elseif(is_readable($directory))
17     {
18         $directory_tree = array();
19         $directory_list = opendir($directory);
20         while($file = readdir($directory_list))
21         {
22             if($file != '.' && $file != '..')
23             {
24                 $path = $directory.'/'.$file;
25                 if(is_readable($path))
26                 {
27                     $subdirectories = explode('/',$path);
28                     if(is_dir($path))
29                     {
30                         $directory_tree[] = array(
31                             'path'      => $path,
32                             'name'      => end($subdirectories),
33                             'kind'      => 'directory',
34                             'content'   => scan_directory_recursively($path, $filter));
35                     }elseif(is_file($path))
36                     {
37                         $extension = end(explode('.',end($subdirectories)));
38                         if($filter === FALSE || $filter == $extension)
39                         {
40                             $directory_tree[] = array(
41                             'path'        => $path,
42                             'name'        => end($subdirectories),
43                             'extension' => $extension,
44                             'size'        => filesize($path),
45                             'kind'        => 'file');
46                         }
47                     }
48                 }
49             }
50         }
51         closedir($directory_list);
52         return $directory_tree;
53     }else{
54         return FALSE;
55     }
56 }
57 // ------------------------------------------------------------
58
59 ?>

top of page

© 2014 lixlpixel