2014-01-23 2 views
0

다른 폴더가 생성되는 "content"폴더가 있으며이 폴더에는 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; 
} 
: 여기

내가 생각하고있는 코드입니다
관련 문제