2011-03-17 5 views

답변

3

당신은 아주 잘 글로브를 사용하지만, 나중에 그것을 정렬 할 몇 가지 추가 코드가 필요 할 수 있습니다

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

이 당신에게 양식 $filename => $timestamp의 연관 배열을 줄 것이다.

+0

글롭은'safe_mode'를 사용하여 일부 시스템에서 사용하지 못할 수 있습니다. ([정보] (http://seclists.org/fulldisclosure/2005/Sep/1)) – drudge

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 
     ) 

) 
관련 문제