问题描述
我不确定这将是多么简单,但是我正在使用一个脚本来显示特定文件夹中的文件,但是我希望按字母顺序显示它们,这样做很难吗? ?这是我正在使用的代码:
I'm not sure how simple this would be, but I'm using a script which displays the files from a specific folder, however I'd like them to be displayed in alphabetical order, would it be hard to do this? Here's the code I'm using:
if ($handle = opendir($mainframe->getCfg( 'absolute_path' ) ."/images/store/")) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (($file != "index.html")&&($file != "index.php")&&($file != "Thumbs.db")) {
$strExt = end(explode(".", $file));
if ($strExt == 'jpg') {
$Link = 'index.php?option=com_shop&task=deleteFile&file[]='.$file;
$thelist .= '<tr class="row0"><td nowrap="nowrap"><a href="'.$Link.'">'.$file.'</a></td>'."\n";
$thelist .= '<td align="center" class="order"><a href="'.$Link.'" title="delete"><img src="/administrator/images/publish_x.png" width="16" height="16" alt="delete"></a></td></tr>'."\n";
}
}
}
}
closedir($handle);
}
echo $thelist;
:)
推荐答案
您可以使用scandir
(文档),默认情况下按字母顺序排序.
Instead of using readdir
you could simply use scandir
(documentation) which sorts alphabetically by default.
scandir
的返回值是一个数组,而不是字符串,因此必须对代码进行一些调整,以遍历该数组,而不是检查最终的null
返回值.另外,scandir
接受带有目录路径的字符串而不是文件句柄作为输入,新版本看起来像这样:
The return value of scandir
is an array instead of a string, so your code would have to be adjusted slightly, to iterate over the array instead of checking for the final null
return value. Also, scandir
takes a string with the directory path instead of a file handle as input, the new version would look something like this:
foreach(scandir($mainframe->getCfg( 'absolute_path' ) ."/images/store/") as $file) {
// rest of the loop could remain unchanged
}
这篇关于PHP(文件夹)文件列表按字母顺序排列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!