Php 按最近的顺序对图像文件进行排序

Php 按最近的顺序对图像文件进行排序,php,image,sorting,directory,Php,Image,Sorting,Directory,问了这个问题,但仍然没有得到适合这个问题的正确代码。我不想更改代码的原因是分页系统工作正常 我尝试了arsort、rsort和许多其他排序功能,但图像仍然没有显示我目录中最近的图像 <?php $page = ""; $record_count = 100; $dir = ('uploaded/'); $offset = ($page-1)*$record_count; $files = glob("uploaded/*.*"); $files_filter = array(arsort

问了这个问题,但仍然没有得到适合这个问题的正确代码。我不想更改代码的原因是分页系统工作正常

我尝试了
arsort
rsort
和许多其他排序功能,但图像仍然没有显示我目录中最近的图像

<?php
$page = "";
$record_count = 100;
$dir = ('uploaded/');
$offset = ($page-1)*$record_count;
$files = glob("uploaded/*.*");
$files_filter  = array(arsort($files,$record_count));//made sorting changes here
$images = array(arsort(glob($dir . '*.*', GLOB_BRACE)));

$latestimage = $images[0];

    $large = '';  
    $allow = array('jpg','jpeg','gif','png', 'JPEG', 'JPG','GIF','PNG'); 


    $i=0; 
    $open = opendir($dir); 
    // get each filename from directory 
    while (($file=readdir($open))!==false) { 
        // get extension 
        $ext=str_replace('.', '', strrchr($file, '.')); 
        // does it have a valid extension 
        if (in_array($ext, $allow))  
          $list[$i++]=$file; // store valid filename in array. use numerical indexing as makes it easier to display paginated images later 
    } 

    $perPage=20; // number of images to show per page 
    $total=count($list); // total number of images to show 
    $pages=ceil($total/$perPage); // number of pages is the number of images divided by how many per page 

    $thisPage=isset($_GET['pg'])?$_GET['pg']-1:0; // did user select a specific page? Note, pages on web are counted from 1 (not zero) so must subtract 1 for correct indexing 
    $start=$thisPage*$perPage; // calculate starting index into list of filenames 

    $perRow=2; // how many images to be shown on each row 

    // display quick index to pages. all pages except current page output as a link 
    print "Page "; 
    for ($i=0;$i<$pages;$i++) 
      if ($i==$thisPage) 
        print "&nbsp;".($i+1); 
      else 
        print "&nbsp;<a href='?pg=".($i+1)."'>".($i+1)."</a>"; 

    print "<tr>"; 
    $imgCnt=0; // used to count number of images displayed and hence whether to wrap page. note, could use "for" index $i but this is computationally quicker 
    for ($i=$start;$i<$start+$perPage;$i++) { 
      // may be too few images to fill page, so check if we have a valid array index. if we don't output empty table cell so fussy browsers 
      // don't mis-display table due to missing cells 
      if (isset($list[$i])) 
        print "<td><a target='_new' href='$dir$large{$list[$i]}'><img  style='height:180px;width:180px; border:2px solid black;  margin:20px 0px 10px  10px; *margin:10px 0px 10px 20px;' style='border-color:#000000 ' border='1' src='$dir{$list[$i]}'></a></td>"; 
      else 
        print "<td></td>"; 

      $imgCnt+=1; // increment images shown 
      if ($imgCnt%$perRow==0) // if image count divided by number to show per row has no remainder than it's time to wrap 
        print "</tr><tr>"; 
    } 
    print "</tr>"; 

    closedir($open); 
?> 

您应该在修改时间为列表编制索引,并在需要显示数组时对其进行切片

更新:您应该使用
krsort

// the paths
$dir     = '/var/www/uploads/';
$urlPath = 'http://localhost/uploads/';

$allow   = array('jpg','jpeg','gif','png', 'JPEG', 'JPG','GIF','PNG');
$open    = opendir($dir); 

while( $file = readdir( $open ) ){

  $ext = strtoupper( pathinfo( $file, PATHINFO_EXTENSION ) );

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

    $modifyTime = filemtime( $dir . $file );
    $list[ $modifyTime ] = $file;

  }
}

# reverse sort on key
krsort( $list );

$perPage  = 20;
$total    = count($list);
$pages    = ceil($total/$perPage);
$thisPage = isset($_GET['pg'])?$_GET['pg']-1:0;
$start    = $thisPage*$perPage;

echo "Page "; 

// show pages
for ($i=0;$i<$pages;$i++):

  if ($i==$thisPage) :
    print "&nbsp;".($i+1); 
  else :
    print "&nbsp;<a href='?pg=".($i+1)."'>".($i+1)."</a>"; 
  endif;

endfor;

// show images
$items = array_slice( $list, $start, $perPage );
foreach( $items as $image ){
  echo "<br/> " . $image . "<br/>";
  echo "<a target='blank' href='" . $urlPath . $image . "'><img width='100' height='100' src='" . $urlPath . $image . "'/></a>";
  echo "<br/>";
}

closedir($open); 
//路径
$dir='/var/www/uploads/';
$urlPath=http://localhost/uploads/';
$allow=array('jpg','jpeg','gif','png','jpeg','jpg','gif','png');
$open=opendir($dir);
而($file=readdir($open)){
$ext=strtoupper(pathinfo($file,pathinfo_扩展名));
if(在数组中($ext$allow)){
$modifyTime=filemtime($dir.$file);
$list[$modifyTime]=$file;
}
}
#按键反向排序
krsort($列表);
每页$20;
$total=计数($list);
$pages=ceil($total/$perPage);
$thisPage=isset($\u GET['pg'])?$\u GET['pg']-1:0;
$start=$thisPage*$perPage;
呼应“页面”;
//显示页面

对于($i=0;$iSome错误显示,一个图像未显示,但图像的其余部分已找到,并且似乎分类不同。分类不同,但最近的图像未显示shown@Eddie_正在使用
arsort
而不是
krsort
。请尝试此更新的代码。它可以工作,我只希望图像可以单击,但是谢谢anyways@Eddie_您可以使用
标记包装
标记,它们将可单击:)