homepage Welcome to WebmasterWorld Guest from 50.19.155.235
register, login, search, subscribe, help, library, PubCon, announcements, recent posts, open posts,
Pubcon Website
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library : Charter : Moderators: coopster & eelixduppy & jatar k

PHP Server Side Scripting Forum

    
Keep 10 latest files in folder
ahmed24




msg:4523177
 12:28 pm on Nov 28, 2012 (gmt 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

 

rainborick




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

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.

brotherhood of LAN




msg:4523316
 6:50 pm on Nov 28, 2012 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
WebmasterWorld ® and PubCon ® are a Registered Trademarks of Pubcon Inc.
© Pubcon Inc. 1996-2012 all rights reserved