note 84036 deleted from function.scandir by danbrown
| From: | danbrown@php.net | Date: | Thu, 28 Jan 2010 14:12:58 +0000 |
| Subject: | note 84036 deleted from function.scandir by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-165439@lists.php.net to get a copy of this message | ||
Note Submitter: Oleg Svarog
----
Some modifications of Daniel's code:
the task before me had such a requirement - to show the folder structure in array as follows
[0] => array
{
[name]=>'smth',
[path] =>'smth',
[sub] => array
{
[0] =>
[1] =>
...
}
}
[1] => array
{
[name]=>'smth',
[path] =>'smth',
[sub] => array
{
[0] =>
[1] =>
...
}
}
...
All the structure will be deposited in $allData array
<?php
/**
* returns the folder content names
*
* @param string $rootDir path to the root folder
* @param array $allowext allowed extensions
* @param array $notallownames not allowed names
* @param array $allData array by reference which collects the root content
*/
static function ScanDirectories($rootDir, $allowext, $notallownames, &$allData)
{
$dirContent = scandir($rootDir);
static $i=0;
foreach($dirContent as $key => $content)
{
if ($content == '.' || $content == '..') continue;
$path = $rootDir . SEPARATOR . $content;
$ext = substr($content, strrpos($content, '.') + 1);
if(in_array($ext, $allowext) && is_file($path) && is_readable($path)
&& !in_array($content, $notallownames))
{
$allData[$i][name] = $content;
$allData[$i][path] = $rootDir;
$allData[$i][sub] = '';
}
else if(is_dir($path) && is_readable($path) && !in_array($content,
$notallownames))
{
$allData[$i][name] = $content;
$allData[$i][path] = $rootDir;
$allData[$i][sub] = array();
self::scanDirectories($path, $allowext, $notallownames, &$allData[$i][sub]);
}
$i++;
}
}
?>