...

View Full Version : Complete directory search script help



kaisellgren
11-13-2006, 01:38 PM
Hi,

It's me again with my wonderful ideas :)

I am trying to make a script that echoes something like this:

folder
another
yes\fold
yes\fold\anotherone
yes\folderagain
justfolder
etc
heehoo\hehSo it basically displayes EVERY folder listed each by one line.
Here's a sample script I have made so far:

function scan_directory($dir)
{
$a = scandir($dir);
$b = array();
foreach ($a as $key)
{
if ($key != "." && $key != ".." && !preg_match("/\\./",$key))
$b[] = $key; // Getting all FOLDERS, not files, to single array
}
$c = array();
foreach ($b as $key)
{
$c[] = scan_directory("$dir/$key");
}
return ($c);
}
$dir = scan_directory("kcms");
print_r($dir);But obviously it's not working corretcly... :(
Here's another one I have tried:

function scan_directory($dir)
{
$a = scandir($dir);
$b = array();
foreach ($a as $key)
{
if ($key != "." && $key != ".." && !preg_match("/\\./",$key))
$b[] = $key; // Getting all FOLDERS, not files, to single array
}
$c = "";
foreach ($b as $key)
{
$c .= scan_directory("$dir/$key");
}
return ($c);
}
$dir = scan_directory("kcms");
echo($dir);But this one outputs nothing! :(

Do anyone have any ideas? I'm doing this script for my own personal use, this script would save A LOT time if I got it working.

Thanks for your time,
Kai

sir pannels
11-13-2006, 02:09 PM
this will do the job for you ...



echo '<h1>Index</h1>';
echo '<ul>';
echo '<li><a href="/"> Parent Directory</a></li>';

$dir = scandir('.');
$files = count( $dir );
for ( $i = 0; $i < $files; $i++ )
{
if ( is_file($dir[$i]) )
echo "<li><a href=\"$dir[$i]\">$dir[$i]</a></li><br>";

if ( is_dir($dir[$i]) )
echo "<li><a href=\"$dir[$i]\"/>$dir[$i]/</a></li><br>";
}

echo '</ul>';

kaisellgren
11-13-2006, 02:13 PM
Thanks, but that does not do the job :(

It only shows the one directory. I need a script that shows all folders from ALL folders. This means COMPLETE directory search, not just one folder... :/

sir pannels
11-13-2006, 02:24 PM
I missed that bit sorry ..

anyway the code is all here :
http://uk.php.net/scandir

the examples are worth working through, there are some examples doing it recursively like you need ..

good luck

Brandoe85
11-13-2006, 03:42 PM
There was an example posted in the post a php forum:
http://www.codingforums.com/showthread.php?t=71882

Good luck;

kaisellgren
11-15-2006, 09:06 PM
Thanks for that link mate, needed! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum