temporary-directory
A lightweight PHP package that simplifies creating, using, and deleting temporary directories on the filesystem. It provides a fluent API to manage temporary storage with automatic cleanup options, commonly used in testing and file processing workflows.
Key facts
Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.
| Field | Value |
|---|---|
| Repository | spatie/temporary-directory |
| Owner | spatie |
| Primary language | PHP |
| License | MIT — OSI-approved |
| Stars | 971 |
| Forks | 47 |
| Open issues | 1 |
| Latest release | 2.4.0 (2026-06-22) |
| Last updated | 2026-06-29 |
| Source | https://github.com/spatie/temporary-directory |
What temporary-directory is
Spatie's TemporaryDirectory is a PHP utility class wrapping OS-level temporary directory operations with a chainable builder pattern. It supports custom locations, permissions, timestamped naming, and automatic deletion on object destruction via destructor hooks.
Get the temporary-directory source
Clone the repository and explore it locally.
git clone https://github.com/spatie/temporary-directory.gitcd temporary-directory# follow the project's README for install & configurationNeed it deployed, integrated, or customized instead? DEV.co ships production installs.
Best use cases
Implementation considerations
- Ensure OS /tmp directory is writable and has sufficient free space; monitor inodes on high-volume systems.
- Use deleteWhenDestroyed() cautiously in long-running daemons or queue workers; prefer explicit delete() calls to avoid race conditions.
- Respect umask and permission() settings; verify expected permissions post-creation, especially on shared hosting with restrictive defaults.
- Handle exceptions from create(), delete(), and empty() methods; document required PHP error handling in your integration.
- Test cleanup behavior in your target environment (Docker, shared hosting, cloud VMs) to confirm /tmp availability and persistence assumptions.
When to avoid it — and what to weigh
- High-Volume Concurrent Temporary Directories — No evidence of thread-safety mechanisms, rate-limiting, or batched cleanup; creating thousands of instances concurrently may exhaust file descriptors or inodes.
- Production Long-Lived Caching — Not designed for durable, persistent caching; prefer dedicated cache drivers (Redis, Memcached) or application-managed storage with lifecycle policies.
- Distributed or Cloud-Native Systems — Relies on local OS /tmp semantics; ephemeral container environments may not persist temporary directories across restarts; use object storage (S3, GCS) instead.
- Multi-Process or Queue Worker Scenarios — Destructor-based cleanup (deleteWhenDestroyed) is unreliable across process boundaries; explicit manual delete() or framework cleanup hooks are required.
License & commercial use
MIT License. Permissive OSI-approved license allowing use, modification, and distribution in proprietary and open-source projects without restriction, provided original copyright notice is retained.
Commercial use is permitted under the MIT License. No restrictions on closed-source or profit-driven applications. No warranty is provided; review the LICENSE file for liability disclaimers. Consider vendor support from Spatie for enterprise deployments.
DEV.co evaluation signals
Editorial assessment — not user reviews. Directional, with an explicit confidence level.
| Signal | Assessment |
|---|---|
| Maintenance | Active |
| Documentation | Strong |
| License clarity | Clear |
| Deployment complexity | Low |
| DEV.co fit | Good |
| Assessment confidence | High |
Temporary directories inherit OS-level filesystem security. No cryptographic operations; relies on OS isolation (Linux permissions, Windows ACLs). Permissions default to 0777; use permission() method to restrict access on multi-user systems. No input validation documented; ensure custom names/paths are sanitized if derived from untrusted input. Destructor-based cleanup may leave orphaned directories if object is not properly garbage-collected; monitor for stale /tmp artifacts. No encryption or secure deletion (e.g., wiping free space); sensitive data should be purged explicitly before deletion.
Alternatives to consider
PHP `sys_get_temp_dir()` + manual cleanup
Lower-level built-in; no abstraction or fluent API; requires manual directory creation, path handling, and recursive deletion.
League/Flysystem
Heavier abstraction for multi-backend file operations; overkill for simple temporary directories but useful if you need cloud storage fallback.
Laravel Storage Facade (temp disk)
Framework-specific; tightly coupled to Laravel; offers similar functionality but less portable to non-Laravel projects.
Build on temporary-directory with DEV.co software developers
Integrate Spatie TemporaryDirectory via Composer. For custom integration, enterprise support, or framework-specific deployment guidance, contact our development team.
Talk to DEV.coRelated open-source tools
Surfaced by semantic similarity across the DEV.co open-source index.
Related on DEV.co
Explore the category and the services that help you build with it.
temporary-directory FAQ
Does this package work with Windows?
Can I use this in a Laravel application?
What happens if I call delete() twice?
Is this safe for concurrent requests in a web server?
Software developers & web developers for hire
From first prototype to production, DEV.co delivers software development services around tools like temporary-directory. Our software development agency staffs experienced software developers and web developers for custom software development, web development, integrations, and ongoing support across open-source testing and beyond.
Ready to streamline temporary file handling in your PHP project?
Integrate Spatie TemporaryDirectory via Composer. For custom integration, enterprise support, or framework-specific deployment guidance, contact our development team.