我如何对php列出的文件进行排序 [英] how do i sort files listed by php
本文介绍了我如何对php列出的文件进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有点困惑我如何对我的结果进行排序.我正在返回目录中的文件列表,并需要以某种方式对它们进行排序...
im a little confused how i can sort my results. Im returning a list of files in a directory and need them to be sorted in some way...
// Start directory
getDirectory('../gallery/photos');
function getDirectory( $path = '.', $level = 0 ){
// Directories to ignore when listing output.
$ignore = array( '.', '..' );
// Open the directory to the handle $dh
$dh = @opendir( $path );
// Loop through the directory
while( false !== ( $file = readdir( $dh ) ) ) {
// Change filename to display date
$displaydate= date('jS M Y', strtotime($file));
// Check that this file is not to be ignored
if( !in_array( $file, $ignore ) ) {
// Indent spacing for better view
$spaces = str_repeat( ' ', ( $level * 5 ) );
// Show directories only
if(is_dir( "$path/$file" ) ){
// Re-call this same function but on a new directory.
// this is what makes function recursive.
echo "$spaces<a href='$path/$file'>$displaydate</a><br />";
getDirectory( "$path/$file", ($level+1) );
}
}
}
// Close the directory handle
closedir( $dh );
}
我将在哪里应用排序功能?
where would i apply the sort function?
谢谢
推荐答案
I recommend scrapping that function and using glob
$files = glob("dir/*");
sort( $files );
这篇关于我如何对php列出的文件进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文