2014-01-23 23 views
0

我有一個文件夾「內容」,其中將生成其他文件夾, 和在這些文件夾中有HTML頁面。 現在我怎麼能打印每個文件夾中的最後修改的html文件?PHP得到最後修改文件,多個文件夾和文件

文件夾前。

content { 
      testfolder1 { file1.html,file2.html ecc..} 
      testfolder2 { file3.html,file4.html ecc..} 
     } 

輸出將是:

file4.html was last insert or modfied 

謝謝,對不起我的英語不好:)

附:在filemtime()函數恨我:d

這是代碼我心目中:

$list = scandir("content"); 
unset($list[0]); 
unset($list[1]); 


foreach($list as $v) 
{ 
    for ($i = 0; $i<=$v; $i++) 
    { 
     $gencat = "content/$v"; 
     $genlist = scandir($gencat); 
     unset($genlist[0]); 
     unset($genlist[1]); 

     foreach($genlist as $k) 
     { 
      $filetime = date("Y/M/D h:i" , filemtime($gencat . "/" . $k)); 
      echo $gencat . "/" . $k . " " . $filetime . "<br/>"; 
     } 
    } 
} 

回答

3

嘛,做如下所示,創建一個函數,通過遍歷所有這些函數並檢查修改的時間來返回上次修改的函數。這個想法是:當你開始迭代時,假設第一個文件是最後一次修改的。繼續迭代,然後在每次迭代中檢查您認爲是最後一次修改的文件。如果之前修改了新的,那就改變了。最後你會有最後修改的。

這是我心目中的代碼:

function lastModifiedInFolder($folderPath) { 

    /* First we set up the iterator */ 
    $iterator = new RecursiveDirectoryIterator($folderPath); 
    $directoryIterator = new RecursiveIteratorIterator($iterator); 

    /* Sets a var to receive the last modified filename */ 
    $lastModifiedFile = "";   

    /* Then we walk through all the files inside all folders in the base folder */ 
    foreach ($directoryIterator as $name => $object) { 
     /* In the first iteration, we set the $lastModified */ 
     if (empty($lastModifiedFile)) { 
      $lastModifiedFile = $name; 
     } 
     else { 
      $dateModifiedCandidate = filemtime($lastModifiedFile); 
      $dateModifiedCurrent = filemtime($name); 

      /* If the file we thought to be the last modified 
       was modified before the current one, then we set it to the current */ 
      if ($dateModifiedCandidate < $dateModifiedCurrent) { 
       $lastModifiedFile = $name; 
      } 
     } 
    } 
    /* If the $lastModifiedFile isn't set, there were no files 
     we throw an exception */ 
    if (empty($lastModifiedFile)) { 
     throw new Exception("No files in the directory"); 
    } 

    return $lastModifiedFile; 
} 
相關問題