Re: code quest
| From: | Daniel P. Brown | Date: | Sat, 27 Nov 2010 17:36:55 +0000 |
| Subject: | Re: code quest | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-309713@lists.php.net to get a copy of this message | ||
On Fri, Nov 26, 2010 at 19:03, Kirk Bailey <kbailey@howlermonkey.net> wrote:
>
> I need a routine that will return a list of every directory immediately
> under the current directory- but nothing else, just a list of directories, 1
> level deep, NO FILES, no listing of current dir or prior dir either.
Simple:
<?php
$ls = scandir(dirname(__FILE__));
foreach ($ls as $d) {
if (is_dir($d) && $d != '.' && $d != '..') {
echo '<a
href="'.$d.'">'.$d.'</a><br/>'.PHP_EOL;
}
}
?>
If you want something more powerful - and often quicker - check
into SPL: specifically FilesystemIterator[1], DirectoryIterator[2],
and RecursiveDirectoryIterator[3]. A quick example to link all child
files and directories with relative linking:
<?php
$path = dirname(__FILE__);
$list = new RecursiveIteratorIterator(new
RecursiveDirectoryIterator($path));
foreach ($list as $k => $v) {
if (!preg_match('/\./',$v)) {
$v = str_replace($path.'/',null,$v); // We only want
relative linking
echo '<a
href="'.$v.'">'.$v.'</a><br/>'.PHP_EOL;
}
}
?>
--
</Daniel P. Brown>
Dedicated Servers, Cloud and Cloud Hybrid Solutions, VPS, Hosting
(866-) 725-4321
http://www.parasane.net/