tbcd / file-archiver
A library to manage files archiving as well as their rotation
Requires
- php: >=8.1
- ext-zip: *
- symfony/filesystem: ^6.0
- symfony/finder: ^6.0
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2025-03-22 19:41:22 UTC
README
FileArchiver is a library to manage files archiving as well as their rotation and their recovery
Table of content
Archivage
Let's suppose we have a file named demo.txt
which we want to archive for 14 days
$fileArchiver = new FileArchiver();
...
$filePath = './demo.txt';
$archivedFile = $fileArchiver->archive($filePath, new DateInterval('P14D'));
And that's it. Your file is archived in the default storage directory __DIR__
but you can specify another location is
you want
$fileArchiver = new FileArchiver('/my/storage/directory');
If you are using Symfony, the default storage location is
%kernel.project_dir%/var/archive/
Suppression and rotation
The archive rotation is proceeded each time you archive a file with the same mask.
For exemple let's imagine we archived our file demo.txt
2 hours ago for a duration of 1 jour. When we will archive our
new file, the previous archived will be deleted because he overlaps his archive duration.
You can anyway clear the archived files the way you want by calling the clear method
// This will clear all files having an expired archive duration
$fileArchiver->clear();
// This will clear all files archived before the passed date
// In the exemple below it will clear all archived files
$fileArchiver->clear(new DateTime());
Recovery
You can recover archived files as below and obtain the list of archived files with the given mask
$archivedFilesPath = $fileArchiver->find('demo');