Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Temporary Directory

Temporary Directory Laravel Package

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.

View on GitHub
Deep Wiki
Context7

A simple class to work with a temporary directory

Frequently asked questions about Temporary Directory
How do I use this package for Laravel background jobs (queues) to ensure temp files are deleted after execution?
Use `deleteWhenDestroyed()` to auto-cleanup when the job completes. Example: `$tempDir = TemporaryDirectory::make()->create()->deleteWhenDestroyed();` Laravel’s job lifecycle ensures cleanup post-execution. For manual control, call `delete()` in the job’s `handle()` or `afterCommit()` hook.
Can I customize the temporary directory path (e.g., use Laravel’s storage instead of `/tmp`)?
Yes. Use `location()` to specify a custom path like `storage_path('temp')`. Example: `TemporaryDirectory::make()->location(storage_path('temp'))->create()`. Note: Ensure the directory is writable across environments (local/dev/prod).
Will this work with Laravel’s filesystem (Storage facade) for storing/retrieving files?
Absolutely. Generate a temp path (e.g., `$tempDir->path('file.txt')`) and use it with `Storage::put()` or `Storage::disk('s3')->put()`. The package handles the directory; Laravel’s filesystem manages the actual storage. Example: `Storage::put($tempDir->path('file.txt'), $content);`.
How do I handle permission errors when creating temp directories on shared hosting?
Shared hosts often restrict `/tmp` permissions. Use `permission()` to set explicit permissions (e.g., `0755`). Example: `TemporaryDirectory::make()->permission(0755)->create()`. Defaults to `0777`, which may fail in restrictive environments.
Is this package safe for high-throughput systems (e.g., bulk file exports) or will directory creation/deletion bottleneck performance?
The package is optimized for performance with minimal overhead. For bulk operations, batch deletions (e.g., `delete()` after processing a batch) or use `deleteWhenDestroyed()` to defer cleanup. Test in staging to validate under load—race conditions are rare due to timestamped directory names.
How do I integrate this with Laravel’s service container for global access?
Bind the package to Laravel’s container in a service provider: `$this->app->singleton(TemporaryDirectory::class, fn() => new TemporaryDirectory());`. Then inject it anywhere: `public function __construct(private TemporaryDirectory $tempDir) {`. This ensures consistent temp dir handling across your app.
Can I use this for Laravel testing (e.g., replacing Storage::fake()) to avoid cluttering `/storage`?
Yes. Create a temp dir in `setUp()` and delete it in `tearDown()`: `$this->tempDir = TemporaryDirectory::make()->create();`. Use `$this->tempDir->path('test.txt')` in tests. This avoids polluting your real storage and ensures isolation.
What Laravel versions and PHP versions are supported?
The package supports PHP 8.1–8.5 and works with all modern Laravel versions (8.x–11.x). It has no Laravel-specific dependencies, so it’s compatible as long as your app meets the PHP version requirement. Tested on Laravel’s LTS releases.
How do I force-create a temp directory if it already exists (e.g., for testing)?
Use the `force()` method to overwrite existing directories: `TemporaryDirectory::make()->force()->create()`. This is useful in CI/CD pipelines or tests where residual directories might linger. Note: Files inside will be deleted without warning.
Are there alternatives to this package for temporary file handling in Laravel?
Native PHP (`sys_get_temp_dir()`, `tempnam()`) or Laravel’s `Storage::fake()` are alternatives, but they lack auto-cleanup and Laravel integration. For more advanced use cases, consider `league/flysystem` with a `Temporary` adapter, though it’s overkill for simple temp dir needs. This package strikes a balance between simplicity and Laravel synergy.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codraw/framework-extra-bundle
codraw/messenger
codraw/security
codraw/mailer
codraw/contracts
codraw/profiling
codraw/dependency-injection
codraw/tester
codraw/core
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony