note 80057 added to function.scandir

From: Date: Thu, 27 Dec 2007 12:28:16 +0000
Subject: note 80057 added to function.scandir
Groups: php.notes 
Request: Send a blank email to php-notes+get-134033@lists.php.net to get a copy of this message
This function generates a list of all files in the chosen directory and all subdirectories, throws them into a NON-multidimentional array and returns them. Most of the recursive functions on this page only return a multi-dimensional array. This is actually a modification of some one else's function (thanks mail at bartrail dot de ;]) <?php function scanDirectories($rootDir, $allData=array()) { // set filenames invisible if you want $invisibleFileNames = array(".", "..", ".htaccess", ".htpasswd"); // run through content of root directory $dirContent = scandir($rootDir); foreach($dirContent as $key => $content) { // filter all files not accessible $path = $rootDir.'/'.$content; if(!in_array($content, $invisibleFileNames)) { // if content is file & readable, add to array if(is_file($path) && is_readable($path)) { // save file name with path $allData[] = $path; // if content is a directory and readable, add path and name }elseif(is_dir($path) && is_readable($path)) { // recursive callback to open new directory $allData = scanDirectories($path, $allData); } } } return $allData; } ?> Example output: print_r(scanDirectories("www")); --- Array ( [0] => www/index.php [1] => www/admin.php [3] => www/css/css.css [4] => www/articles/2007/article1.txt [4] => www/articles/2006/article1.txt [8] => www/img/img1.png ) ---- Server IP: 87.124.64.96 Probable Submitter: 81.158.246.213 ---- Manual Page -- http://www.php.net/manual/en/function.scandir.php Edit -- https://master.php.net/note/edit/80057 Del: integrated -- https://master.php.net/note/delete/80057/integrated Del: useless -- https://master.php.net/note/delete/80057/useless Del: bad code -- https://master.php.net/note/delete/80057/bad+code Del: spam -- https://master.php.net/note/delete/80057/spam Del: non-english -- https://master.php.net/note/delete/80057/non-english Del: in docs -- https://master.php.net/note/delete/80057/in+docs Del: other reasons-- https://master.php.net/note/delete/80057 Reject -- https://master.php.net/note/reject/80057 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#134033) next »