Forum Moderators: coopster

Message Too Old, No Replies

deleting old files from a directory

         

tommy98

12:03 pm on Feb 3, 2010 (gmt 0)

10+ Year Member



Hi there, I am trying to build a script that will delete the oldest files from a folder (keep the latest 5 files)

$count = count(glob($Dir . "*"));
if($count > 5){

// delete all old files except the newest 5
}

?

incrediBILL

7:31 pm on Feb 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You have to get the last modified date from each file and sort them to find the latest 5.

$last_modified = filemtime("example.txt");

jkovar

4:09 am on Feb 7, 2010 (gmt 0)

10+ Year Member



$err = shell_exec('rm `ls -tr | head -n \'-5\'`');

tommy98

5:20 am on Feb 7, 2010 (gmt 0)

10+ Year Member



So where abouts in the shell_exec do I specify the root folder?