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         // 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 ?>

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

top of page

© 2010 lixlpixel