2017-10-10 76 views
0

我有这段代码来显示来自文件夹的图像。所以任何人都知道如何添加函数来按日期将所有图像分类到我的代码中。 我已经测试过放入类似krsort($ file)的东西;或asort($文件); 但我不知道在哪里把它放在工作。任何帮助都会很棒。谢谢。如何按日期排序文件

<?php 
$directory = $words[$lang]["folder_link"]; 
$allowed_types=array('jpg','jpeg','gif','png','JPG'); 
$file_parts=array(); 
$ext=''; 
$title=''; 
$i=0; 

$dir_handle = @opendir($directory) or die("There is an error with your image directory!"); 

while ($file = readdir($dir_handle)) 
{ 
if($file=='.' || $file == '..') continue; 

$file_parts = explode('.',$file); 
$ext = strtolower(array_pop($file_parts)); 

$title = implode('.',$file_parts); 
$title = htmlspecialchars($title); 

$nomargin=''; 

if(in_array($ext,$allowed_types)) 
{ 
    if(($i+1)%4==0) $nomargin='nomargin'; 

    echo ' 
    <div class="pic '.$nomargin.'" style="background:url('.$directory.'/'.$file.') no-repeat 50% 50%;"> 
    <a href="'.$directory.'/'.$file.'" title="'.$title.'" target="_blank">'.$title.'</a> 
    </div>'; 

    $i++; 

} 
} 

closedir($dir_handle); 

?> 
+0

你是什么意思?文件修改日期? – tan

+0

文件修改日期 –

+0

[很多](https://stackoverflow.com/questions/2667065/sort-files-by-date-in-php)[of](https://stackoverflow.com/questions/2084986/file -creation-time)[Qs](https://stackoverflow.com/questions/18089172/sort-file-list-by-date)[cover](https://stackoverflow.com/questions/10021458/sorting-directory -files-by-creation-date-in-php)[this](https://stackoverflow.com/questions/33767060/how-to-sort-by-date-using-php-opendir)[topic](https) ://stackoverflow.com/questions/124958/glob-sort-by-date)[已经](https://stackoverflow.com/questions/7948300/order-this-array-by-date-modified)(from a快速搜索)。 – salathe

回答

0

首先存储文件中使用filemtime为重点的新数组。然后使用kso​​rt对结果进行排序。

<?php 
$directory = $words[$lang]["folder_link"]; 
$allowed_types=array('jpg','jpeg','gif','png','JPG'); 
$file_parts=array(); 
$ext=''; 
$title=''; 
$i=0; 

$dir_handle = @opendir($directory) or die("There is an error with your image directory!"); 
$fileset = [] ; 
while ($file = readdir($dir_handle)) { 
    if ($file == '.' || $file == '..') 
     continue; 

    $file_parts = explode('.', $file); 
    $ext = strtolower(array_pop($file_parts)); 

    $title = implode('.', $file_parts); 
    $title = htmlspecialchars($title); 

    $nomargin = ''; 

    if (in_array($ext, $allowed_types)) { 

     $fileset[] = ['time' => filemtime($file) , 'file' => $file ] ; 
    } 
} 
closedir($dir_handle); 

usort($fileset, function ($item1, $item2) { 
    return $item1['time'] <=> $item2['time']; 
    // $item1['file'] //do file comparison if necessary 
// return $item2['time'] <=> $item1['time']; //ascending 
}); 


foreach($fileset as $a) { 
    $file = $a['file'] ;  
     if (($i + 1) % 4 == 0) 
      $nomargin = 'nomargin'; 

     echo ' 
    <div class="pic ' . $nomargin . '" style="background:url(' . $directory . '/' . $file . ') no-repeat 50% 50%;"> 
    <a href="' . $directory . '/' . $file . '" title="' . $title . '" target="_blank">' . $title . '</a> 
    </div>'; 

     $i++; 
} 

?> 
+0

谢谢,但我现在只能看到一个图像。你可以把它放到原始代码中,这样我就可以看到我没有把这些代码放在错误的地方。谢谢。 –

+0

@PatrikIdén修改后的代码,请检查。 – tan

+0

不,这只显示1张图片。 –