2011-03-17 52 views

回答

3

可以很好地使用水珠,但需要一些額外的代碼在事後對其進行排序:

$files = glob("subdir/*"); 
$files = array_combine($files, array_map("filemtime", $files)); 
arsort($files); 

這會給你的形式​​的關聯數組。

+0

水珠可以使用'safe_mode'一些系統被禁用。 ([信息](http://seclists.org/fulldisclosure/2005/Sep/1)) – drudge 2011-03-18 00:03:57

1

不太漂亮,但可允許額外的功能:

<?php 
    // directory to scan 
    $dir = 'lib'; 

    // put list of files into $files 
    $files = scandir($dir); 

    // remove self ('.') and parent ('..') from list 
    $files = array_diff($files, array('.', '..')); 

    foreach ($files as $file) { 
     // make a path 
     $path = $dir . '/' . $file; 

     // verify the file exists and we can read it 
     if (is_file($path) && file_exists($path) && is_readable($path)) { 
      $sorted[] = array(
       'ctime'=>filectime($path), 
       'mtime'=>filemtime($path), 
       'atime'=>fileatime($path), 
       'filename'=>$path, 
       'filesize'=>filesize($path) 
      ); 
     } 
    } 

    // sort by index (ctime) 
    asort($sorted); 

    // reindex and show our sorted array 
    print_r(array_values($sorted)); 
?> 

輸出:

Array 
(
    [0] => Array 
     (
      [ctime] => 1289415301 
      [mtime] => 1289415301 
      [atime] => 1299182410 
      [filename] => lib/example_lib3.php 
      [filesize] => 36104 
     ) 

    [1] => Array 
     (
      [ctime] => 1297202755 
      [mtime] => 1297202722 
      [atime] => 1297202721 
      [filename] => lib/example_lib1.php 
      [filesize] => 16721 
     ) 

    [2] => Array 
     (
      [ctime] => 1297365112 
      [mtime] => 1297365112 
      [atime] => 1297365109 
      [filename] => lib/example_lib2.php 
      [filesize] => 57778 
     ) 

) 
相關問題