PHP Delete Files Larger Than 1 Megabyte (MB)

You can change 1048576 for your files. It represents 1 MB.

With this code you can scan and delete all files in all directories and subdirectories.

<?php

function listFolderFiles($dir){
    $ffs = scandir($dir);

    unset($ffs[array_search('.', $ffs, true)]);
    unset($ffs[array_search('..', $ffs, true)]);

    // prevent empty ordered elements
    if (count($ffs) < 1)
        return;


    foreach($ffs as $ff){

        if (strstr($dir,".git")){
            continue;
        }
        if (filesize($dir.'/'.$ff) > 1048576 ){
            echo $dir.'/'.$ff . "\n";
            unlink($dir.'/'.$ff);
        }

        if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);

    }

}

listFolderFiles('.');

Leave a Reply

Your email address will not be published. Required fields are marked *