我有一个文件夹“content”,其中会生成其他文件夹, 在这些文件夹中有html页面。 现在我如何在每个文件夹中打印最后修改过的html文件?
文件夹ex。
content {
testfolder1 { file1.html,file2.html ecc..}
testfolder2 { file3.html,file4.html ecc..}
}
输出将是:
file4.html was last insert or modfied
谢谢,抱歉我的英语不好:)
P.S。 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/>";
}
}
}
答案 0 :(得分: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;
}
答案 1 :(得分:0)
$filename = 'somefile.txt';
if (file_exists($filename)) {
echo "$filename was last modified: "
. date ("F d Y H:i:s.", filemtime($filename));
}
你可以检查:
http://br1.php.net/manual/en/function.filemtime.php http://br1.php.net/manual/en/directoryiterator.getmtime.php