note 60383 added to function.scandir

From: Date: Thu, 05 Jan 2006 14:03:17 +0000
Subject: note 60383 added to function.scandir
Groups: php.notes 
Request: Send a blank email to php-notes+get-101249@lists.php.net to get a copy of this message
I made a function that scans the directory and all it subfolders. You can also sort them with folders first and files after, in alphabetic order. It works with both PHP 4 and 5. <?php function rec_scandir($dir) { $files = array(); if ( $handle = opendir($dir) ) { while ( ($file = readdir($handle)) !== false ) { if ( $file != ".." && $file != "." ) { if ( is_dir($dir . "/" . $file) ) { $files[$file] = rec_scandir($dir . "/" . $file); }else { $files[] = $file; } } } closedir($handle); return $files; } } function cmp($a, $b) { if ( is_string($a) && is_string($b) ) { return strcmp($a, $b) > 0 ? 1 : -1; }elseif ( is_int($a) && is_int($b) ) { return $a > $b ? 1 : -1; }elseif ( is_int($a) && is_string($b) ) { return 1; }elseif ( is_string($a) && is_int($b) ) { return -1; }else { return 0; } } function array_ukmultisort(&$arr, $func) { uksort($arr, $func); while ( list($key, $val) = each($arr) ) { if ( is_array($val) ) { array_ukmultisort($arr[$key], $func); } } } $dir = rec_scandir("."); array_ukmultisort($dir, "cmp"); echo "<pre>"; print_r($dir); echo "</pre>"; ?> ---- Manual Page -- http://www.php.net/manual/en/function.scandir.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+60383 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60383&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60383&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60383&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60383&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60383&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60383&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60383&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+60383&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#101249) next »