Skip to content
Advertisement

listing files in folder showing index.php

I have this code, but it is showing the index.php itself How can I filter *.php files?

<?php
    if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle)))
    {
        if ($file != "." && $file != "..")
        {
            $thelist .= '<LI><a href="'.$file.'">'.$file.'</a>';
        }
    }
    closedir($handle);
    }
?>

<P>Dir:</p>
<UL>
<P><?=$thelist?></p>
</UL>

Also is there a way to sort them by modification or creation time?

Advertisement

Answer

Here’s another that uses ksort or krsort functions (tested).
(See comments in code.)

<?php
// you can add to the array
$ext_array = array(".htm", ".php", ".asp", ".js"); //list of extensions not required
$dir1 = "."; 
$filecount1 = 0; 
$d1 = dir($dir1);

while ($f1 = $d1->read()) { 
$fext = substr($f1,strrpos($f1,".")); //gets the file extension
if (in_array($fext, $ext_array)) { //check for file extension in list
continue;
}else{
if(($f1!= '.') && ($f1!= '..')) { 
if(!is_dir($f1)) $filecount1++;

$key = filemtime($f1);
$files[$key] = $f1 ;
} 
}
}

// use either ksort or krsort => (reverse order)
//ksort($files);
krsort($files);

foreach ($files as $f1) {
$thelist .= '<LI><a href="'.$f1.'">'.$f1.'</a>';
}

?>

<P>Dir:</p>
<UL>
<P><?=$thelist?></p>
</UL>
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement