note 38435 added to function.scandir
| From: | triray at ziprite dot com | Date: | Sun, 21 Dec 2003 04:51:02 +0000 |
| Subject: | note 38435 added to function.scandir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-62282@lists.php.net to get a copy of this message | ||
Here's another directory scanner. I copied it originally from this site and added the optional
recurse and optional sort order. It returns full paths to dirs and full paths+filename. Check the
possible sort flags in the online manual for the sort() function.
Use it like this:
$scan = array();
$files = array();
$dirs = array();
$scan = scan_dir("directory name",TRUE or FALSE,sort flag);
$files = $scan['files'];
$dirs = $scan['directories'];
echo "\n These are the files:\n\n";
foreach($files as $key => $val)
{echo" $val\n";}
echo "\n These are the directories:\n\n";
foreach($dirs as $key => $val)
{echo" $val\n";}
exit;
//******************************************************
// This functions was named list_dir by whoever
// originally wrote it.
// if this is your code. Thanks for the start.
//*******************************************************
function scan_dir($dirname,$recurse=FALSE,$sort_flag=SORT_REGULAR)
{
if($dirname[strlen($dirname)-1]!='/')
{$dirname.='/';}
static $file_array=array();
static $dir_array=array();
static $ret_array=array();
$handle=opendir($dirname);
while (false !== ($file = readdir($handle)))
{
if($file=='.'||$file=='..')
continue;
if(is_dir($dirname.$file))
{
$dir_array[]=$dirname.$file;
if($recurse)
{
scan_dir($dirname.$file.'/',$recurse);
}
}
else
$file_array[]=$dirname.$file;
}
closedir($handle);
sort($file_array,$sort_flag);
sort($dir_array,$sort_flag);
reset($file_array);
reset($dir_array);
$ret_array['files']=$file_array;
$ret_array['directories']=$dir_array;
return $ret_array;
}
----
Manual Page -- http://www.php.net/manual/en/function.scandir.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+38435
Delete -- http://master.php.net/manage/user-notes.php?action=delete+38435&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+38435&report=yes
Search -- http://master.php.net/manage/user-notes.php