您好我有这个代码来显示php中文件夹的图像:
$handle = opendir(dirname(realpath(__FILE__)).'/galerija/accomodation/');
while($file = readdir($handle)) {
if($file !== '.' && $file !== '..') {
echo '<img src="galerija/accomodation/'.$file.'" rel="colorbox" />';
}
}
它正在工作,但我怎么能设置按名称或类似的方式显示文件夹分类器,因为我真的需要对图像进行排序,这个脚本只显示随机图像。谢谢。
答案 0 :(得分:1)
答案 1 :(得分:0)
您应首先将图像($files
)存储到数组中,例如$aImages[] = $file
。您可以使用PHP中的多个排序函数来对数组进行排序。 asort(), usort(), sort()...
。见http://php.net/manual/en/ref.array.php
答案 2 :(得分:0)
你应该在这里找到答案: Sorting files by creation/modification date in PHP
还有其他类似的帖子,你可以获得另一个有用的功能进行排序。
这样你的代码应该是这样的:
if($h = opendir(dirname(realpath(__FILE__)).'/galerija/accomodation/')) {
$files = array();
while(($file = readdir($h) !== FALSE){
if($file !== '.' && $file !== '..'){
$files[] = stat($file);
}
}
// do the sort
usort($files, 'sortByName');
// do something with the files
foreach($files as $file) {
echo '<img src="galerija/accomodation/'.$file.'" rel="colorbox" />';
}
}
//some functions you can use to sort the files
//sort by change time
//you can change filectime with filemtime and have a similar effect
function sortByChangeTime($file1, $file2){
return (filectime($file1) < filectime($file2));
}
function sortByName{
return (strcmp($file1,$file2));
}