我有这个代码:
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".." && strtolower(substr($file, strrpos($file, '.') + 1)) == 'html')
{
$patterns = array();
$patterns[0] = '/_/';
$patterns[1] = '/.html/';
$patterns[2] = '/index/';
$replacements = array();
$replacements[2] = ' ';
$replacements[1] = '';
$replacements[0] = 'Strona główna';
$wynik = preg_replace($patterns, $replacements, $file);
$newVariable = str_replace("_", " ", $file);
$thelist .= '<li><a href="'.$file.'">'.ucfirst($wynik).'</a></li>';
}
}
closedir($handle);
}
?>
<P>List of files:</p>
<P><?=$thelist?></p>
有没有办法按字母顺序显示文件列表?现在脚本列出了它所在目录中的 html 文件。如何修改我可以手动设置要读取的目录的脚本?
//按字母顺序编码:
<?php
if ($handle = opendir('.')) {
$files = glob("*");
foreach ($files as $file) // replace `while` with `foreach`
{
if ($file != "." && $file != ".." && strtolower(substr($file, strrpos($file, '.') + 1)) == 'html')
{
$patterns = array();
$patterns[0] = '/_/';
$patterns[1] = '/.html/';
$patterns[2] = '/index/';
$replacements = array();
$replacements[2] = ' ';
$replacements[1] = '';
$replacements[0] = 'Strona główna';
$wynik = preg_replace($patterns, $replacements, $file);
$newVariable = str_replace("_", " ", $file);
$thelist .= '<li><a href="'.$file.'" target="_blank">'.ucfirst($wynik).'</a></li>';
}
}
closedir($handle);
}
?>