Welcome to WebmasterWorld Guest from 54.167.10.145

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Keep 10 latest files in folder

     
12:28 pm on Nov 28, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:July 17, 2006
posts:137
votes: 0


I've got a folder called Photos, i want a php script to keep only the 10 latest files and delete everything else.

Any ideas how I can achieve this?

Thanks
6:14 pm on Nov 28, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2003
posts:904
votes: 5


Crawl the directory and use filemtime() to check the last modification time on each file. The function returns a UNIX timestamp, which you can easily sort.
6:50 pm on Nov 28, 2012 (gmt 0)

Moderator from GB 

WebmasterWorld Administrator brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 30, 2002
posts:4842
votes: 1


If you're on Linux, something like

ls -t | head -10


the -t flag in ls orders them by file modification time, if by that you mean 'latest' rather than file creation time.

head -10 is saying take the top 10 off the list.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members