dongshike7171 2015-07-28 05:10
浏览 29
已采纳

循环浏览一系列文件夹并检查文件夹的年龄

I am making a Cron that will delete a folder older than 15days. I already made a function to delete the folder and it's content, what I don't have is to loop inside a folder then check each folder's age then if that is 15days old or above I will executue my delete function.

I want to loop inside public/uploads

in my uploads directory I store folders with content ex.

public/
  uploads/
      Test/
      Test2/

I want to check how old those folder then delete it by calling

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;
}

How do I do that? Thanks

展开全部

  • 写回答

1条回答 默认 最新

  • duanleixun2439 2015-07-28 05:20
    关注

    The function you are looking for is filemtime(). This lets you determine the last modified date of a file (or directory). That in combination with the various directory functions will allow you to loop through them and check their dates.

    This is something mocked up off the top of my head to give you a rough idea of how you may go about this:

    $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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部