https://github.com/bpolaszek/oldfilesremover
A simple class to remove all files older than a DateTime instance.
https://github.com/bpolaszek/oldfilesremover
Last synced: 11 months ago
JSON representation
A simple class to remove all files older than a DateTime instance.
- Host: GitHub
- URL: https://github.com/bpolaszek/oldfilesremover
- Owner: bpolaszek
- License: mit
- Created: 2014-03-27T15:52:50.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2014-03-27T16:18:07.000Z (almost 12 years ago)
- Last Synced: 2025-01-10T09:59:50.196Z (about 1 year ago)
- Size: 141 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
OldFilesRemover
===============
A simple class to remove all files older than a DateTime instance.
In fact, you can choose between 2 classes :
- OldFilesRemover for removing files on a single-level
- OldFilesRemoverRecursive for removing files recursively
Usage for OldFilesRemover :
```php
$MyDir = new DirectoryIterator('/path/to/explore');
$Expired = new DateTime('yesterday midnight');
$OldFilesRemover = new OldFilesRemover($MyDir, $Expired);
// You can have a look at which files are going to be deleted.
var_dump($OldFilesRemover->getFiles());
// You can also iterate on these files to do something else
foreach ($OldFilesRemover AS $file)
var_dump($file) // DirectoryIterator object
// Will delete all files older than yesterday, midnight
$NbRemovedFiles = $OldFilesRemover->deleteOldFiles();
// One-line Short-hand
OldFilesRemover::NewInstance($MyDir, $Expired)->deleteOldFiles();
```
Usage for OldFilesRemoverRecursive :
```php
// Basically the same, but the constructor prefers a RecursiveIteratorIterator
$MyDir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('/path/to/explore'),
RecursiveIteratorIterator::SELF_FIRST);
$Expired = new DateTime('-1 year');
$Extensions = array('csv', 'pdf'); // For deleting only csv and pdf files
// Will recursively delete all csv and pdf files older than 1 year from today
OldFilesRemoverRecursive::NewInstance($MyDir, $Expired, $Extensions)->deleteOldFiles();
```