How to delete files from directory based on creation date in php?

前端 未结 8 1844
囚心锁ツ
囚心锁ツ 2020-11-29 03:52

I have a cache folder that stores html files. They are overwritten when needed, but a lot of the time, rarely used pages are cached in there also, that just end up using sp

8条回答
  •  无人及你
    2020-11-29 04:27

    /* Detele 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() - 3600) { // 1 hour
        unlink($file);
        }
    }
    

    I am using this, hope it helps.

提交回复
热议问题