2014-03-31 74 views
1

整理文件,我現在用的是下面的PHP代碼從一個文件夾logfiles_patient從文件夾

$path = "logfiles_patient/"; 
    // Open the folder 
    $dir_handle = @opendir($path) or die("Unable to open $path"); 
    // Loop through the files 
    while ($file = readdir($dir_handle)) 
    { 
     if(($file!='.')&&($file!='..')) 
     { 
    echo "<a target='_blank' href='log_Patient_download.php?filename=$file'>$file</a>"; 
     } 
    } 
    // Close 
    closedir($dir_handle); 

和輸出呼應得到的所有文件是

March 19, 2014.txt 
March 20, 2014.txt 
March 21, 2014.txt 

我要重新排列的輸出作爲

March 21, 2014.txt 
March 20, 2014.txt 
March 19, 2014.txt 
+0

是否文件名與文件創建日期對應? –

+0

是.Filename是文件創建的日期 – Arvie

回答

1

您可以暫時將您的$file var存儲在array中,並在while循環後應用sorting functions

像這樣:

$array = array();  

while ($file = readdir($dir_handle)) 
{ 
    if(($file!='.')&&($file!='..')) 
      $array[] = $file; 
} 

$array = arsort($array); 

foreach($array as $file) 
    echo "<a target='_blank' href='log_Patient_download.php?filename=$file'>$file</a>"; 
3

我建議你試試這樣:

$files = glob('logfiles_patient/*'); 
if(is_array($files)){ 
    foreach ($files as $file){ 
     $coll[basename($file)] = filemtime($file); 
    } 
    asort($coll); 
    $files = array_keys($coll); 
} 

請記住,如果​​3210遇到錯誤,它將返回false