note 84036 added to function.scandir
| From: | OlegSvarog at osu1 dot php dot net | Date: | Wed, 25 Jun 2008 07:41:38 +0000 |
| Subject: | note 84036 added to function.scandir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-140786@lists.php.net to get a copy of this message | ||
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++;
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 89.28.15.172
----
Manual Page -- http://www.php.net/manual/en/function.scandir.php
Edit -- https://master.php.net/note/edit/84036
Del: integrated -- https://master.php.net/note/delete/84036/integrated
Del: useless -- https://master.php.net/note/delete/84036/useless
Del: bad code -- https://master.php.net/note/delete/84036/bad+code
Del: spam -- https://master.php.net/note/delete/84036/spam
Del: non-english -- https://master.php.net/note/delete/84036/non-english
Del: in docs -- https://master.php.net/note/delete/84036/in+docs
Del: other reasons-- https://master.php.net/note/delete/84036
Reject -- https://master.php.net/note/reject/84036
Search -- https://master.php.net/manage/user-notes.php