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.
/storage).Storage facade) and event-driven workflows (e.g., queues, commands). The deleteWhenDestroyed() feature complements Laravel’s dependency injection and service container lifecycle.use Spatie\TemporaryDirectory\TemporaryDirectory and chain methods (e.g., ->create()->path('file.txt')).->location(storage_path('temp'))).->permission(0755)).->force()).0777 may conflict with shared hosting (solved via permission()).deleteWhenDestroyed() relies on PHP’s garbage collection (tested in CI).storage_path('app') for all temporary files, or only specific workflows (e.g., exports)?/tmp quotas on shared hosts)?delete()), automatic (deleteWhenDestroyed()), or event-driven (e.g., Laravel’s terminating middleware)?storage_path('temp')) behave across local/dev/prod?->delete() in tearDown)?Storage facade (e.g., Storage::put($tempDir->path('file.txt'), $content)).Storage::fake() for file-based tests (e.g., TemporaryDirectory::make()->path('test.txt')).FilesystemIterator for deletion)./tmp paths in a CSV export job:
// Before
$tempFile = tempnam(sys_get_temp_dir(), 'export_');
// After
$tempDir = TemporaryDirectory::make()->create();
$tempFile = $tempDir->path('export.csv');
public function __construct(private TemporaryDirectory $tempDir) {}
$this->app->singleton(TemporaryDirectory::class, fn() => new TemporaryDirectory());
sys_get_temp_dir(), wrap calls in a facade or helper:
function tempPath(string $filename): string {
return TemporaryDirectory::make()->create()->path($filename);
}
->location(storage_path('temp')) to avoid /tmp restrictions./tmp) have sufficient space.tempnam()/sys_get_temp_dir() calls.Storage::fake() where applicable).0777 vs. 0755 issues in prod.->empty() first).->name('unique_prefix') or ->force().$tempDir->create();
Log::debug('Temp dir created at:', [$tempDir->getPath()]);
mkdir).FilesystemIterator ensures robust cleanup (even with symlinks).deleteWhenDestroyed() relies on PHP’s garbage collection (tested to ~10K dirs/sec)./tmp or custom paths for quotas.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Directory not deleted | Disk space exhaustion | Use try-catch with delete(); log failures. |
Permission denied (0777) |
Write operations fail | Set ->permission(0755) or adjust hosting. |
| Race condition (same name) | Overwritten data | Use ->name(uniqid()) or ->force(). |
| PHP garbage collection delay | Zombie directories | Call unset($tempDir) or ->delete(). |
Hosting /tmp quota exceeded |
Job failures | Use ->location(storage_path('temp')). |
deleteWhenDestroyed() vs. manual delete().TemporaryDirectory to verify cleanup:
$tempDir = $this->createMock(TemporaryDirectory::class);
$tempDir->method('delete')->willReturn(true);
->delete() in tearDown).How can I help you explore Laravel packages today?