PHP

Find Files Modified in Last N Days

admin by @admin ADMIN
1d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
List every file under a directory that was modified within the last N days. Useful for incremental backups or detecting stale entries.
PHP
Raw
<?php
function recentFiles(string $root, int $days): array {
    $cutoff = time() - $days * 86400;
    $out    = [];
    $it = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($root, FilesystemIterator::SKIP_DOTS)
    );
    foreach ($it as $file) {
        if ($file->isFile() && $file->getMTime() >= $cutoff) {
            $out[] = $file->getPathname();
        }
    }
    return $out;
}

print_r(recentFiles('/var/www', 1));  // everything touched in the last 24h
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.