Rainer
Rainer

Reputation: 7

Recursively delete all files/folders older than 14days?

My hoster offers PHP as scripting language and I want to setup a CRON. The CRON is basically a GUI where I can enter the script path and script language (PHP). I need a small script which runs recursively over all folders and files, checking the age and deleting all files older than e.g. 14days. The PHP needs to be standalone, so that I can be copied to the .php file. PHP version is up to 5.4.

Upvotes: 0

Views: 441

Answers (1)

Rouven Weßling
Rouven Weßling

Reputation: 631

I'm not a big fan spoon-feeding whole scripts, but here are some pointers.

You can get a list of all files and folders in a path using an RecursiveDirectoryIterator, passing this to an RecursiveIteratorIterator can make the tree structure flat so you can loop trough it. For a small script I'd probably just filter inside that loop, if you think you might need more flexibility (for example pluggable strategies) later on you should check out FilterIterator and RecursiveCallbackFilterIterator.

Upvotes: 1

Related Questions