note 46082 added to function.readdir

From: Date: Tue, 28 Sep 2004 22:46:38 +0000
Subject: note 46082 added to function.readdir
Groups: php.notes 
Request: Send a blank email to php-notes+get-77542@lists.php.net to get a copy of this message
Yet another recursive directory lister :P Why did I create it? I like to keep things small, and it seems like all the scripts around here are a lot bigger and more complex while they produce the same (or nearly the same) output. This one creates a multidimensional array with the folders as associative array and the files as values. It puts the name of the folders and files in it, not the complete path (I find that annoying but its easy to change that if you prefer it with the path). And I used a for loop instead of a while loop like everyone around here does. Why? 1. I just like for loops. 2. Everyone else uses a while loop. <?php function ls($dir){ $handle = opendir($dir); for(;(false !== ($readdir = readdir($handle)));){ if($readdir != '.' && $readdir != '..'){ $path = $dir.'/'.$readdir; if(is_dir($path)) $output[$readdir] = ls($path); if(is_file($path)) $output[] = $readdir; } } return isset($output)?$output:false; closedir($handle); } print_r(ls('.')); ?> PS: if you don't need the files in it then you can just comment out the "if(is_file....." part. ---- Manual Page -- http://www.php.net/manual/en/function.readdir.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+46082 Delete -- http://master.php.net/manage/user-notes.php?action=delete+46082&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+46082&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#77542) next »