/    Sign up×
Community /Pin to ProfileBookmark

Delete Files Efficientlly

Hi All,

I have the following code which I intend to run once or twice a week to delete old files from my web servers uploads folder.

[code=php]$dir = ‘../uploads’;
if (is_dir($dir)) $files = scandir($dir);
foreach ($files as $file) {
if ($file == ‘.’ || $file == ‘..’) continue;
if (fileatime($dir . ‘/’ . $file) < strtotime(‘-90 days’)) unlink($dir . ‘/’ . $file);
}[/code]

I am interested in getting the best performance possible from this script as there are many thousands of files in the uploads folder.

I this code the most efficient way of getting the job done?


Thanks,
dai.hop

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 01.2010 — Do the strtotime() before the loop, and assign the result to a variable; then use that variable within the loop, so that you don't have to call the strtotime() function on every iteration.

Also, I might try using readdir() (instead of scandir) and compare its performance. It would not require populating an array with every file, so it [i]might[/i] work better with "thousands" of files -- or it might not, depending on available memory and other factors beyond my knowledge. ?
Copy linkTweet thisAlerts:
@dai_hopauthorOct 01.2010 — Thanks for the tips, I'll modify my script accordingly.
×

Success!

Help @dai_hop spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...