spatie/temporary-directory
Create, use, and automatically clean up temporary directories in PHP. Spatie TemporaryDirectory makes it easy to generate a temp folder (in your system temp path), build file paths inside it, and delete everything when you’re done.
Start by installing the package via Composer:
composer require spatie/temporary-directory
This package solves the common problem of safely managing temporary directories in CLI scripts, console commands, or background jobs. The simplest use case is creating, using, and cleaning up temporary files in /tmp (or your system's temp directory) without worrying about leftover artifacts. Get started with:
use Spatie\TemporaryDirectory\TemporaryDirectory;
$tempDir = (new TemporaryDirectory())->create();
// Write something
file_put_contents($tempDir->path('data.json'), json_encode(['status' => 'ok']));
// Clean up
$tempDir->delete();
The most common first use case is processing large or sensitive intermediate data (e.g., file conversions, caching, test fixtures) where cleanup is non-negotiable.
(new TemporaryDirectory('/custom/path'))
->name('my-job-'.uniqid())
->permission(0750)
->create();
deleteWhenDestroyed(): Wrap usage in a function or scope and let PHP’s GC handle deletion:
public function process(User $user): void
{
$dir = (new TemporaryDirectory())->deleteWhenDestroyed()->create();
// Build temp files...
$this->generateReport($dir->path('report.pdf'));
// No manual delete needed!
}
make() for brevity:
$dir = TemporaryDirectory::make();
empty() for resets without full teardown:
$dir->empty(); // wipes contents, keeps directory
$dir->path('new-output.csv'); // safe to use again
handle() to ensure temp directories don’t persist across runs:
$tmp = TemporaryDirectory::make(storage_path('app/temp'));
$tmp->create();
// ... process
$tmp->delete();
name() without force() throws on conflict: If you specify a custom name, the package throws an exception if the directory already exists. Use ->force() to overwrite — but do so only when safe (e.g., names include timestamps or UUIDs).->permission() after create() won’t affect existing directories. Always chain it before create().getName() is available but fragile: Added in v2.3.0, getName() returns just the directory name — but relies on internal path parsing. Don’t assume the name matches your name() input if it was auto-generated (e.g., timestamp).deleteWhenDestroyed() in CLI tools, where process termination guarantees cleanup even if deletion fails.strict() was implied) caused leftover dirs on exceptions. Since v2.0+, the library avoids this — but always pair create() with either delete() or deleteWhenDestroyed().sys_get_temp_dir(); avoid hardcoding /tmp or C:\Temp. Use $tempDir->path() for subpaths.empty() caution: empty() uses FilesystemIterator — it deletes only direct contents. Subdirectories are not recursively removed (a common gotcha when building nested paths).^1.3 — but be aware it lacks recent security and robustness fixes (e.g., strict mode, symlink fixes).How can I help you explore Laravel packages today?