Shelve files in directory structure according to years/months/days
PHP package to shelve files in directory structures according to years/months/days.
I.e. takes a file
data.txt and copies it to
composer require cstuder/temporal-shelf
<?php require('vendor/autoload.php'); $shelf = new \cstuder\TemporalShelf\TemporalShelf($targetDirectory); $shelvedFilename = $shelf->shelveFile($filename);
docs/example.php for a full working example.
temporal-shelf library copies files into a shelf (a file archive). A shelf is a directory structure with the default path containing current year, month and day. Additionally the file gets a prefix containing the current timestamp.
I.e.: The file
download.txt is shelved to
The directory structure is generated automatically, with permissions
Both path (
$directoryPattern) and prefix (
$prefixPattern) can be configured with a string which is then passed through the PHP DateTime::format method.
$directoryPattern = "Y/W" and
$prefixPattern = "N_" would shelve the file to
/archive/2020/43/3_download.txt (Generating a weekly directory per year).
By default will throw an exceptionn when a file is already exists in the shelve with the same target name. This can be configured with the
Exceptions are thrown when the file is not readable, when the directory structure cannot be built or if the copying fails.
Files are copied. The original file is left untouched and has to be cleaned up by yourself.
The library is intentionally kept simple and doesn't handle non-alphabetical directory or file prefix patterns well. Also keep the shelf directory unpolluted by other files.
__construct(string $shelfDirectory, string $directoryPattern = 'Y/m/d', string $filePrefixPattern = 'U_', string $timezone = 'UTC', int $overwriteOption = Options\OverwriteOptions::EXCEPTION_ON_OVERWRITE)
Creates the TemporalShelf object and sets the configuration.
$shelfDirectory is the target archive directory root.
$directoryPattern is the pattern of the subdirectories where the files are to be sorted in.
$filePrefixPattern is the prefix added to the filename when shelving. Set it to
'' in order to disable the prefixing.
$timezone is a timezone identifier used when converting the timestamp of the file to the directory path.
$overwriteOption determines the behaviour when the target filename already exists in the shelf directory.
Does not validate the shelf directory.
Shelves a file with the current configuration and an optional timestamp. If the timestamp is null, takes the current time.
Returns the full path to the shelved file.
Returns an array of paths to all files in the shelf directory. All files, not just shelved files.
$sortOrder determines the order of the array (
Returns the path to the freshest file on the shelf. Returns
null if no file is found.
composer test to execute the PHPUnit test suite.
- Add changes to the changelog.
- Create a new tag
Christian Studer firstname.lastname@example.org, Bureau für digitale Existenz.