Get most recent modified time of a folder recursively with PHP I'm relatively new to php and any advice is appreciated.

I have written a code to the most recently updated time for a folder in php.
Can you please correct me if I am doing anything wrong.

And here the code I use:

function get_m_time_dir ($directory)
{
$last_modified_time = 0;
$handler = opendir ($directory);
while ($file = readdir ($handler))
{
    if ($file == '.' || $file == '..')
    {
        continue;
    }
    if (is_file ($directory . DIRECTORY_SEPARATOR . $file))
    {
        $filemtime = filemtime ($directory . DIRECTORY_SEPARATOR . $file);
        if ($filemtime &gt; $last_modified_time)
        {
            $last_modified_time = $filemtime;
        }
    } else
    {
        $filemtime = get_m_time_dir ($directory . DIRECTORY_SEPARATOR . $file);
        if ($filemtime &gt; $last_modified_time)
        {
            $last_modified_time = $filemtime;
        }
    }
}
return $last_modified_time;
}
Can you please correct me if I am doing anything wrong.


It depends on whether you care for the deleted files or not. If you do, you should also check the modification time for the directory itself, not only files in it. 

The code itself could be also improved:   


you can remove the duplicated condition
which in turn, can be substituted with max() function.
also you can utilize the glob() function that can save you that boring $file == '..' condition, as well as other unnecessary code.


So in the end the code could be

function get_m_time_dir ($directory)
{
    $last_modified_time = 0;
    $dirmtime = filemtime ($directory);
    foreach (glob("$directory/*") as $file)
    {
        if (is_file ($file))
        {
            $filemtime = filemtime ($file);
        } else {
            $filemtime = get_m_time_dir ($file);
        }
        $last_modified_time = max($filemtime, $dirmtime, $last_modified_time);
    }
    return $last_modified_time;
}