PHP Remove x Minutes old file – recursive

0.5K views
no comments
10 Jul 2018 2:29 am

I need that function to delete 10 minutes old files from my project,
remove_x_minutes_old_files function required two parameters $path for directory path and $in_minutes for minutes how much old file you want to delete.

function remove_x_minutes_old_files($path,$in_minutes=10){
    $minutes_in_seconds = $in_minutes*60;
    $now_unix_time = strtotime('now');
    $d2 = array('.','..');
    $dirs = array_diff(scandir($path),$d2);
    foreach($dirs as $pathKey=>$pathVal){
        $new_path =$path.$pathVal;
        if(is_dir($new_path)){
            remove_ten_minutes_old_files($new_path.'/');
        } else {
            if(($now_unix_time - filemtime($new_path) ) >=$minutes_in_seconds){
               unlink($new_path);
            }
        }
    }
}

NOTE:Your Email Address will be not shown and please do not add spamming comments because here is REL="NOFOLLOW" on your links and comments also moderated shown.
<code>Put html css or any language code under this tag</code>