The correct way to delete all files older than 2 days in PHP

后端 未结 9 2353
一生所求
一生所求 2020-12-05 01:56

Just curious

        $files = glob(cacheme_directory().\"*\");
        foreach($files as $file)
        {
            $filemtime=filemtime ($file);
                  


        
相关标签:
9条回答
  • 2020-12-05 02:17

    Be aware that you'll run into problems if you have a very large number of files in the directory.

    If you think this is likely to affect you, consider using a lower level approach such as opendir.

    0 讨论(0)
  • 2020-12-05 02:30

    Looks correct to me. I'd just suggest you replace 172800 with 2*24*60*60 for clarity.

    0 讨论(0)
  • 2020-12-05 02:33
    /* Delete Cache Files Here */
    $dir = "cache/"; /** define the directory **/
    
    /*** cycle through all files in the directory ***/
    foreach (glob($dir."*") as $file) {
    //foreach (glob($dir.'*.*') as $file){
    
    /*** if file is 24 hours (86400 seconds) old then delete it ***/
    if (filemtime($file) < time() - 172800) { // 2 days
        unlink($file);
        }
    }
    

    Hope it help you.

    0 讨论(0)
提交回复
热议问题