note 82455 added to function.glob

From: Date: Fri, 11 Apr 2008 15:31:47 +0000
Subject: note 82455 added to function.glob
Groups: php.notes 
Request: Send a blank email to php-notes+get-138185@lists.php.net to get a copy of this message
As a follow up to recursively determining all paths (by viajy at yoyo dot org) and opendir being faster than glob (by Sam Yong - hellclanner at live [dot] com). The list all dirs code didn't seem to work, at least on my server (provided by parazuce [at] gmail [dot] com). I needed a function to create an unlimited multidimensional array, with the names of the folders/files intact (no realpath's, although that is easily possible). This is so I can simply loop through the array, create an expandable link on the folder name, with all the files inside it. This is the correct way to recurse I believe (no static, return small arrays to build up the multidimensional array), and includes a check for files/folders beginning with dots. // may need modifications function list_files($path) { $files = array(); if(is_dir($path)) { if($handle = opendir($path)) { while(($name = readdir($handle)) !== false) { if(!preg_match("#^\.#", $name)) if(is_dir($path . "/" . $name)) { $files[$name] = list_files($path . "/" . $name); } else { $files[] = $name; } } closedir($handle); } } return $files; } print_r(list_files("/path/to/folder")); // example usage function list_html($list) { $html = ""; foreach($list as $folder => $file) { if(is_array($list[$folder])) { $html .= "> (folder) " . $folder . "<br>"; $html .= list_html($list[$folder]); } else { $html .= " (file) " . $file . "<br>"; } } return $html; } echo list_html(list_files("/path/to/folder")); ---- Server IP: 66.207.199.35 Probable Submitter: 70.69.46.77 ---- Manual Page -- http://www.php.net/manual/en/function.glob.php Edit -- https://master.php.net/note/edit/82455 Del: integrated -- https://master.php.net/note/delete/82455/integrated Del: useless -- https://master.php.net/note/delete/82455/useless Del: bad code -- https://master.php.net/note/delete/82455/bad+code Del: spam -- https://master.php.net/note/delete/82455/spam Del: non-english -- https://master.php.net/note/delete/82455/non-english Del: in docs -- https://master.php.net/note/delete/82455/in+docs Del: other reasons-- https://master.php.net/note/delete/82455 Reject -- https://master.php.net/note/reject/82455 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#138185) next »