php - 列出显示 index.php 的文件夹中的文件

标签 php dir

我有这段代码,但它显示的是 index.php 本身 我如何过滤 *.php 文件?

<?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>

还有没有办法按照修改或创建时间对它们进行排序?

最佳答案

这是另一个使用 ksortkrsort 函数(已测试)。
(请参阅代码中的注释。)

<?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>

关于php - 列出显示 index.php 的文件夹中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15555771/

相关文章:

php - 将自定义 "Booking"按钮添加到 WooCommerce 产品单页

java - 在 ant 的另一个目录中运行 java 时出错

linux - 文件系统加速 - 'find' 命令

php - 如果记录存在,Laravel with() 关系

php - 如何使用数组的多个键/值进行 SQL 查询?

php - Wordpress 查询运行两次

java - Android - 使用扫描仪读取文件,如何获取文件

windows - 命令列出文件夹中的所有文件以及 Windows 中的子文件夹

excel - 将工作簿另存为 *.xml 到子文件夹时的目录问题

php - MySQL GROUP BY UNIX 时间戳