Temper offers a smoother approach to working with temp-files in PHP.
There are two ways to use it:
- 'Consume' a temp-file: create, use, and remove a temp-file in a single operation
- Create and remove temp-files in separate steps
$temper = new Temper('/path/to/tmp/dir');
$temper->consumeFile(function (SplFileInfo $tempFileinfo): void {
// Do something with temp-file
});
// Temp-file gone
$temper->consumeFile(function (SplFileInfo $tempFileinfo): void {
// Do something with `.jpg` temp-file
}, 'jpg');
// Temp-file gone
Note
Since, at the end of its life, a Temper instance will automatically clean-up any remaining temp-files it knows about, you may never need to call cleanUp()
by hand
$temper = new Temper('/path/to/tmp/dir');
$tempFileWithoutExtension = $temper->createFile();
$tempImageFile = $temper->createFile('jpg');
// Removes *all* remaining temp-files created by the Temper instance
$temper->cleanUp();
Install using Composer:
composer require danbettles/temper