Answers for "php if 2 files in dir unlink the olderst"

PHP
0

php if 2 files in dir unlink the olderst

<?php
    $directory = ".";

    $files = array();
    foreach(scandir($directory) as $file){
        if(is_file($file)) {

            //get all the files
            $files[$file] = filemtime($file);
        }
    }

    //sort descending by filemtime;
    arsort($files);
    $count = 1;
    foreach ($files as $file => $time){
        if($count > 10){
            unlink($file);
        }
        $count++;
    }
Posted by: Guest on November-19-2020

Browse Popular Code Answers by Language