Scansione ricorsiva di una directory con PHP
Open Source, PHP, Tips & tricks 17 Giugno 2008, 0:55
Ci sono script che dovresti sempre avere a portata di mano, perché finisce che ne hai bisogno nei momenti e nelle occasioni più impensabili. Questa function in PHP, ad esempio, mi ha risparmiato in molti casi un’inutile perdita di tempo: passandole come argomento una stringa che corrisponde al percorso completo da scandire, la function scan_directory_recursively restituisce un array contenente la struttura completa della directory specificata.
È possibile indicare, come secondo argomento opzionale, una estensione o una porzione di testo che non dovrà necessariamente comparire nel nome del file. Si tratta in pratica di un filtro per escludere una certa classe di risultati.
Ad esempio:
$dirs=scan_directory_recursively('/home/ivan/Tempo');
restituisce un array di tutte le directory e i file contenuti in /home/ivan/Tempo, mentre:
$dirs=scan_directory_recursively('/home/ivan/Tempo','.php');
restituisce un array di tutte le directory e i file contenuti in /home/ivan/Tempo ad eccezione dei file con estensione .php!
Non è opera mia, per questo motivo chiedo a chiunque ne faccia uso nei propri progetti di non rimuovere i riferimenti a lixlpixel, il sito Internet da cui provengono.
Ecco la function:
< ?php // ------------ lixlpixel recursive PHP functions ------------- // scan_directory_recursively( directory to scan, filter ) // expects path to directory and optional an extension to filter // of course PHP has to have the permissions to read the directory // you specify 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'); function scan_directory_recursively($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 is not 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 set 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; } } // ------------------------------------------------------------ ?>
Per comodità vostra, ma anche mia, aggiungo qui una versione priva di commenti e pronta per il download:















