遍历一系列文件夹并检查文件夹的年龄

Loop through series of folders and check how old the folder

我正在制作一个 Cron,它将删除超过 15 天的文件夹。我已经做了一个删除文件夹及其内容的功能,我没有的是在一个文件夹内循环然后检查每个文件夹的年龄然后如果它是 15 天或以上我将执行我的删除功能。

我想在里面循环public/uploads

在我的 uploads 目录中,我存储了内容为 ex.

的文件夹
public/
  uploads/
      Test/
      Test2/

我想检查这些文件夹的历史,然后通过调用

将其删除
function Delete($path)
{
 if (is_dir($path) === true)
 {
    $files = array_diff(scandir($path), array('.', '..'));

    foreach ($files as $file)
    {
        Delete(realpath($path) . '/' . $file);
    }

    return rmdir($path);
 }

 else if (is_file($path) === true)
 {
    return unlink($path);
 }

 return false;
}

我该怎么做?谢谢

您正在寻找的函数是 filemtime(). This lets you determine the last modified date of a file (or directory). That in combination with the various directory functions 将允许您遍历它们并检查它们的日期。

这是我脑海中模拟出来的东西,目的是让您大致了解如何您可以这样做:

$dir = '/path/to/my/folders';
$folders = scandir($dir);
foreach ($folders as $folder) {
    $lastModified = filemtime($folder);
    // Do a date comparison here and call your delete if necessary
}