0

I have a folder "content", in which other folders will be generated, and in these folders there are html pages. Now how I can print the last modified html file in each folder?

folder ex.

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

the output will be:

file4.html was last insert or modfied

Thanks and sorry for my bad English :)

p.s. the filemtime() function hate me :D

This is the code I have in mind:

$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/>";
        }
     }
}

2 Answers 2

3

Well, do as follows, create a function that returns the last modified by iterating over all of them and checking the modified time. The idea is: when you start to iterate, suppose the first file is the last modified. Continue to iterate, and then in each iteration check the file you think to be the last modified against the new one. If the new one was modified earlier, you change that. In the end you'll have the last modified one.

Here's the code I have in mind:

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0
$filename = 'somefile.txt';
if (file_exists($filename)) {
    echo "$filename was last modified: "
    . date ("F d Y H:i:s.", filemtime($filename));
}

you can check:

https://www.php.net/manual/en/function.filemtime.php https://www.php.net/manual/en/directoryiterator.getmtime.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.