DEV.co
Open-Source Testing · spatie

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.

Source: GitHub — github.com/spatie/temporary-directory
971
GitHub stars
47
Forks
PHP
Primary language
MIT
License (OSI-approved)

Key facts

Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.

FieldValue
Repositoryspatie/temporary-directory
Ownerspatie
Primary languagePHP
LicenseMIT — OSI-approved
Stars971
Forks47
Open issues1
Latest release2.4.0 (2026-06-22)
Last updated2026-06-29
Sourcehttps://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.

Quickstart

Get the temporary-directory source

Clone the repository and explore it locally.

terminalbash
git clone https://github.com/spatie/temporary-directory.gitcd temporary-directory# follow the project's README for install & configuration

Need it deployed, integrated, or customized instead? DEV.co ships production installs.

Best use cases

Unit and Integration Testing

Use temporary directories in test suites to isolate file I/O operations, avoid filesystem pollution, and ensure cleanup via deleteWhenDestroyed() without manual teardown.

Data Processing Pipelines

Stage uploads, logs, or intermediate exports in isolated temporary storage before moving to permanent locations, reducing risk of partial writes or orphaned files.

Local Development and Debugging

Quickly scaffold sandboxed file storage for rapid prototyping without cluttering project directories or worrying about stale test artifacts.

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.

SignalAssessment
MaintenanceActive
DocumentationStrong
License clarityClear
Deployment complexityLow
DEV.co fitGood
Assessment confidenceHigh
Security considerations

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.

Software development agency

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.co

Related 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?
Unknown. README does not specify OS compatibility. Temporary directory behavior differs between Unix-like systems (/tmp) and Windows (TEMP/TMP env vars). Requires testing or documentation review.
Can I use this in a Laravel application?
Yes. The package is framework-agnostic and fully compatible with Laravel. It integrates well with PHPUnit test suites and Laravel's testing conventions.
What happens if I call delete() twice?
Unknown from provided data. Likely throws an exception if the directory no longer exists, but error handling should be reviewed in the source code.
Is this safe for concurrent requests in a web server?
Partially. Each request gets its own temporary directory (via timestamping), but cleanup relies on destructor timing, which is non-deterministic in web contexts. Explicit delete() calls recommended in request handlers.

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.