Archive of the day June 17, 2008

Evolution (Linux Commercial)

1 commento » Miscellaneous , YouTube 1 Comment »


Recursive scan a directory with PHP

Nessun commento » Open Source , PHP , Tips & Tricks No Comments »

There are scripts that you should always have on hand, because it ends up that you need in the most unexpected moments and occasions. This function in PHP, for example, saved me in many cases an unnecessary waste of time passing as argument a string that is the full path to scan the scan_directory_recursively function returns an array containing the complete structure of the specified directory.

You can indicate, as the second optional argument, an extension or a portion of text that will not necessarily appear in the file name. This is basically a filter to exclude a certain class of results.

For example:

$dirs=scan_directory_recursively('/home/ivan/Tempo');

returns an array of all directories and files in / home / ivan / time, while:

$dirs=scan_directory_recursively('/home/ivan/Tempo','.php');

returns an array of all directories and files in / home / ivan / time except for files with extension. php!

It is not my doing, why I ask those who make use in their projects do not remove references to lixlpixel, the Internet site from which they come.

Here's the function:


 <? Php

 / / Recursive PHP functions ------------- ------------ lixlpixel

 / / Scan_directory_recursively (directory to scan, filter)

 / / Expects path to the directory and an optional extension to filter

 / / Of course PHP has to have the permissions to read the directory

 / / Specify you and all files and folders inside this directory

 / / ------------------------------------------------ ------------


 / / To use this function to get all files and directories in an array, write:

 / / $ Filestructure scan_directory_recursively = ('path / to / directory');


 / / To use this function to scan a directory and filter the results, write:

 / / $ FileSelection scan_directory_recursively = ('directory', 'extension');


 scan_directory_recursively function ($ directory, $ filter = FALSE)

 {
    
 / / If the path has a slash at the end we remove it here
    
 if (substr ($ directory, -1) == '/')
    
 {
        
 $ Directory = substr ($ directory, 0, -1);
    
 }

    
 / / If the path is not valid or not is a directory ...
    
 if (file_exists ($ directory) | |! is_dir ($ directory))
    
 {
        
 / / ... 
 we return false and exit the function
        
 return FALSE;

    
 / / ... 
 else if the path is readable
    
 } Elseif (is_readable ($ directory))
    
 {
        
 / / We open the directory
        
 $ Directory_list = opendir ($ directory);

        
 / / And scan through the items inside
        
 while (FALSE! == ($ file = readdir ($ directory_list)))
        
 {
            
 / / If the filepointer is not the current directory
            
 / / Or the parent directory
            
 if ($ file! = '.' && $ file! = '..')
            
 {
                
 / / We build the new path to scan
                
 $ Path = $ directory. '/'. $ File;

                
 / / If the path is readable
                
 if (is_readable ($ path))
                
 {
                    
 / / We split the new path by directories
                    
 $ Subdirectories = explode ('/', $ path);

                    
 / / If the new path is a directory
                    
 if (is_dir ($ path))
                    
 {
                        
 / / Add the directory details to the file list
                        
 $ Directory_tree [] = array (
                            
 'Path' => $ path,
                            
 'Name' => end ($ subdirectories),
                            
 'Kind' => 'directory',

                            
 / / We scan the new path by calling this function
                            
 'Content' => scan_directory_recursively ($ path, $ filter));

                    
 / / If the new path is a file
                    
 } Elseif (is_file ($ path))
                    
 {
                        
 / / Get the file extension by taking everything after the last dot
                        
 $ Extension = end (explode ('.', End ($ subdirectories)));

                        
 / / If there is no filter in September or the filter is set and matches
                        
 if ($ filter === FALSE | | $ filter == $ extension)
                        
 {
                            
 / / Add the file details to the file list
                            
 $ Directory_tree [] = array (
                                
 'Path' => $ path,
                                
 'Name' => end ($ subdirectories),
                                
 'Extension' => $ extension,
                                
 'Size' => filesize ($ path),
                                
 'Kind' => 'file');
                        
 }
                    
 }
                
 }
            
 }
        
 }
        
 / / Close the directory
        
 closedir ($ directory_list); 

        
 / / Return file list
        
 return $ directory_tree;

    
 / / If the path is not readable ...
    
 Else {}
        
 / / ... 
 we return false
        
 return FALSE;
    
 }

 }

 / / ------------------------------------------------ ------------


 ?>

For your convenience, but also my own, I add here a version without the comments and ready for download: