我正在使用以下php扫描目录并输出文件。
我需要按字母顺序排序,但不确定如何做到这一点。
这是我到目前为止所拥有的:
<?php
// path to directory
$directory = "gallery/photos/";
// open the directory
$handle = openDir($directory);
// Read the directory
while ($file = readDir($handle)) {
// filter the directory
if ($file != "." && $file != ".." && !is_dir($file)) {
// Allow only images (filter)
if (strstr($file, ".gif") || strstr($file, ".png") || strstr($file, ".PNG") || strstr($file, ".jpg")) {
// Path to the actual file
$directory_file = $directory . $file;
// Get image information (width, height)
$info = getImageSize($directory_file);
// show the picture
echo "<img src='"$directory_file'" data-title='"$file'"";
echo " width='"$info[0]'" height='"$info[1]'"> <br>'n";
}
}
}
// Close the directory
closeDir($handle);
?>
改用 scandir 按字母顺序返回文件
$arr = array_diff( scandir ( $directory ), ['.', '..'] );
// We are remmoving . and .. at this place
foreach($file in $arr){
// do your stuff
}