3

我有这段代码,但它显示 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>

还有一种方法可以按修改或创建时间对它们进行排序吗?

4

4 回答 4

2

只需将另一个排除项添加到您忽略“。”的部分。和 '..' 例如:

if ($file != "." && $file != ".." && !preg_match('/\.php$/i', $file))

这将排除任何最后带有 .php 的文件。

于 2013-03-21T18:55:21.627 回答
2

这是另一个使用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>
于 2013-03-21T20:52:48.677 回答
1
<?php
    if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle)))
    {
        if ($file != "." && $file != ".." && substr(strrchr($file,'.'),1) != 'php')
        {
            $thelist .= '<LI><a href="'.$file.'">'.$file.'</a>';
        }
    }
    closedir($handle);
    }
?>
于 2013-03-21T18:57:39.553 回答
1

(我构建的一些东西)
这将向您显示带有扩展名的文件名以及文件计数(已测试)

<?php
// you can add to the array
$ext_array = array(".htm", ".php", ".asp", ".js");
//list of extensions not required (above)
$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++;

$thelist .= '<LI><a href="'.$f1.'">'.$f1.'</a>';

} 
}
}

// add text and count number below files
echo "Total files in folder: ";
echo "$filecount1";
?>

<P>Dir:</p>
<UL>
<P><?=$thelist?></p>
</UL>
于 2013-03-21T19:14:55.713 回答