note 100710 added to function.readdir

From: Date: Mon, 01 Nov 2010 15:46:09 +0000
Subject: note 100710 added to function.readdir
Groups: php.notes 
Request: Send a blank email to php-notes+get-173666@lists.php.net to get a copy of this message
A variation on listing all the files in a directory recursively. The code illustrates a basic technique : the use of an auxiliary function. It avoids building temporary lists which are merged on the way back. Note that the array which collects the information must be passed by reference. function listdir($dir='.') { if (!is_dir($dir)) { return false; } $files = array(); listdiraux($dir, $files); return $files; } function listdiraux($dir, &$files) { $handle = opendir($dir); while (($file = readdir($handle)) !== false) { if ($file == '.' || $file == '..') { continue; } $filepath = $dir == '.' ? $file : $dir . '/' . $file; if (is_link($filepath)) continue; if (is_file($filepath)) $files[] = $filepath; else if (is_dir($filepath)) listdiraux($filepath, $files); } closedir($handle); } $files = listdir('.'); sort($files, SORT_LOCALE_STRING); foreach ($files as $f) { echo $f, "\n"; } ---- Server IP: 69.147.83.197 Probable Submitter: 193.248.162.29 (proxied: 127.0.0.1) ---- Manual Page -- http://www.php.net/manual/en/function.readdir.php Edit -- https://master.php.net/note/edit/100710 Del: integrated -- https://master.php.net/note/delete/100710/integrated Del: useless -- https://master.php.net/note/delete/100710/useless Del: bad code -- https://master.php.net/note/delete/100710/bad+code Del: spam -- https://master.php.net/note/delete/100710/spam Del: non-english -- https://master.php.net/note/delete/100710/non-english Del: in docs -- https://master.php.net/note/delete/100710/in+docs Del: other reasons-- https://master.php.net/note/delete/100710 Reject -- https://master.php.net/note/reject/100710 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#173666) next »