Weave 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

Spatie's PHP package quickly creates, uses, and deletes temporary directories in the system's temp folder. Chainable methods handle custom naming, path helpers, and automatic cleanup of every file inside when the directory is deleted.

View on GitHub
Deep Wiki
Context7

Easily create, use and destroy temporary directories

Frequently asked questions about Temporary Directory
How do I install spatie/temporary-directory in Laravel?
Run composer require spatie/temporary-directory from your Laravel project root. There is no service provider, config file, migration, or facade to register. You can use the TemporaryDirectory class immediately in Laravel, plain PHP, or CLI workers.
Which Laravel and PHP versions does spatie/temporary-directory support?
It requires PHP 8.0 or newer, and v2 dropped PHP 7.x support. The package has no Laravel version constraint and no transitive dependencies, so it fits Laravel 10 through 12 on PHP 8.1+ without dragging in version conflicts. Its changelog and releases are consistently maintained, including PHP 8.5 CI coverage.
What are common use cases for spatie/temporary-directory in a Laravel app?
Common uses are transient artifacts such as generated reports and PDFs, CSV imports and exports, media transcode staging, backup assembly, and test fixtures. Use it for scratch files during a request or job, then move any durable output to object storage or a Laravel disk. It should not become a shadow persistence layer for data that must survive the request.
Is spatie/temporary-directory a replacement for Laravel Storage or Flysystem?
No. spatie/temporary-directory complements Flysystem and Laravel Storage::disk(), which should hold anything that must survive the request or job. Use the temporary directory for staging and scratch data only, and write the durable copy to a storage disk.
How do I create a temporary directory with a custom name?
Call name() with your desired directory name and then create(); use force() only when you deliberately need to override an existing directory. Explicit names combined with force() are a collision hazard in parallel queue workers, so timestamped default names are safer. Use the path() helper to build file paths inside the directory.
How does automatic cleanup work if a Laravel job crashes or times out?
Cleanup runs when you call delete(), or automatically when the object is destroyed if you use deleteWhenDestroyed(). Fatal errors, OOM kills, and job timeouts can bypass both paths and leave orphaned directories that silently grow disk usage. Monitor scratch disk space and wrap cleanup in try/finally when it must be guaranteed.
What permissions does spatie/temporary-directory use and can I configure them?
The default permission is 0777, which will often fail a security review. The package now exposes a configurable permission() method, so mandate 0755 or your organization's standard in an application wrapper. Also confirm the temp location and permissions are valid in containers and read-only filesystem setups.
Do I need a service provider or config file for spatie/temporary-directory?
No. It is a single-purpose, dependency-free utility class with PSR-4 autoloading and no service provider, config, or facade. That makes it equally usable in Laravel, plain PHP services, and CLI workers.
How should I use spatie/temporary-directory in Laravel tests?
Create a TemporaryDirectory per test or fixture, write files through path(), and delete it in teardown or rely on deleteWhenDestroyed(). A try/finally block keeps cleanup reliable if an assertion or exception interrupts the test. Wrap it in a small app helper if you want the same location and permission defaults across your test suite.
Is spatie/temporary-directory maintained, and what alternatives should I consider?
It is MIT licensed and consistently maintained, with changelog and releases including PHP 8.5 CI coverage. Alternatives include native tempnam() and sys_get_temp_dir() with hand-rolled recursive cleanup, or Laravel File facade helpers for deleting directories. Choose this package when you want an object that owns its lifecycle and path helpers; keep a thin wrapper so a future swap stays isolated.
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.
mehedimi/laravel-amp-mail
happones/laravel-vue-i18n-generator
jwerd/inertia-table
siberfx/flysystem-wasabi
hollyit/token-replace
ippanelcom/laravel-sdk
adachsoft/changelog-linter
adachsoft/collection
payerurl/telegrambot-crypto-payment-instant-payout
dskripchenko/laravel-hybrid-search
riorizkyrainey/lara-ced
mehedimi/static-vel
zharp/zharp
nonsapiens/bigquery-model-sync
lgu-bayambang-ict-office/lgu-laravel-development-standards
andydefer/laravel-support
se7enxweb/flysystem
lampwick/laravel
jeslage/statamic-emojis
micahdshackelford/rmap-laravel