Forum Moderators: coopster

Message Too Old, No Replies

Read folder, putting files in array and display them without dots?

How to display a list of files without "." ".."?

         

dbzfyam

8:18 pm on Aug 18, 2006 (gmt 0)

10+ Year Member



I want to index a folder (only the files inside it), but when I run my code I get this in the array aswell:

.
..

My code:

<?php
$handle = opendir('banners');
while ($file = readdir($handle)) {
$files[] = $file;
foreach ($files as $v) {
echo "{$v}<BR>";
}
}
closedir($handle);
?>

I also tried this:

<?php
$handle = opendir('banners');
while ($file = readdir($handle)) {
if ($file!= "." && $file!= "..") {
$files[] = $file;
foreach ($files as $v) {
echo "{$v}<BR>";
}
}
}
closedir($handle);
?>

And it does get rid of the dots, but now I get the files several times (some duplicates).
Does anyone know hoe to solve this?

Thanks in dvance,
Stefan

eelixduppy

4:16 am on Aug 19, 2006 (gmt 0)



My personal favorite for this:

foreach([url=http://us2.php.net/manual/en/function.glob.php]glob[/url]("*") as $file) {
echo $file.'<br />';
}

Good luck!

phprockz

4:16 am on Aug 19, 2006 (gmt 0)

10+ Year Member



Try this funtion

<?
function dirTree($dir) {
$d = dir($dir);
while (false!== ($entry = $d->read())) {
if($entry!= '.' && $entry!= '..' && is_dir($dir.$entry))
$arDir[$entry] = dirTree($dir.$entry.'/');
}
$d->close();
return $arDir;
}
function printTree($array, $level=0) {
foreach($array as $key => $value) {
$key = str_replace("_", " ", "$key");
echo "&middot; $key";
echo "<br>";
if(is_array($value))
printTree($value, $level+1);
}
}
?>

dbzfyam

2:25 pm on Aug 20, 2006 (gmt 0)

10+ Year Member



Thanks guys! Woking perfectly!