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.
tempnam/mkdir/FilesystemIterator glue. The package wins not on capability but on the long tail of edge cases you would otherwise rediscover in production: broken symlinks on delete, 0777 default permissions with an override, name collisions under concurrent load, and cleanup that survives exceptions. At ~zero cost and MIT license, buying the hardened version is the default-correct call.create(), path(), delete()) prevents each team from inventing its own /tmp convention, which is a recurring source of "disk full at 3 a.m." incidents.deleteWhenDestroyed() is the decision-relevant feature: it lets you make cleanup a property of scope rather than a step a developer must remember. That converts a class of bug (orphaned directories accumulating until the volume fills) into a code-review-level guarantee.location() / constructor location argument plus configurable permission() means the same code works on a laptop, in a container with a read-only root, and on a mounted scratch volume — a decision that otherwise surfaces late, during deployment.Adopt when most of the following hold:
/tmp growing unbounded, or "works locally, fails in the container" cleanup bugs.deleteWhenDestroyed()), rather than dependent on a developer remembering a finally block.path('dumps/datadump.dat') — for handing off to libraries that expect a filesystem path (DomPDF, Intervention Image, PhpSpreadsheet, ZipArchive).Look elsewhere when:
league/flysystem or Laravel's Storage facade; this package is deliberately not that./tmp. Verify the environment before committing; the location() option helps but does not create capacity.tempnam() or Flysystem's temp-file helpers may fit better.Migration/risk notes for the evaluation: the 2.0 release dropped PHP 7 and replaced generic exceptions with dedicated exception classes, so exception handling written against 1.x will silently change behavior. Pin the major version and check that no existing catch (InvalidArgumentException) blocks depend on the old contract. Also confirm your CI matrix matches the package's supported PHP versions.
For executives (30 seconds): We have several features that create throwaway files on disk — generated reports, exports, image processing, bulk imports. Today each one handles cleanup a little differently, and the failure mode is boring but expensive: disks fill up, workers fall over, and someone gets paged. This is a free, MIT-licensed utility from a well-established PHP vendor that makes creating and — critically — guaranteeing the deletion of that scratch space a one-liner. It is a small, low-risk standardization, not a platform bet. Expected outcome: fewer disk-related incidents, faster delivery on the next few file-handling features, and no new vendor, license, or data-handling exposure.
For engineering (the real argument): Stop hand-rolling temp-directory logic. The package gives you a scoped object with create(), path(), empty(), delete(), and — the reason to adopt — deleteWhenDestroyed(), so cleanup happens on scope exit rather than on developer discipline. It handles the parts your ad-hoc code forgets: recursive deletion, broken symlinks, unique directory names under concurrent load, configurable permissions (default 0777, override with permission()), and a custom base location for containerized environments where /tmp is constrained. It throws dedicated exceptions, so "directory already exists" is catchable and force() is an explicit opt-in rather than a race. It is PHP 8+, zero-config, network-free, and likely already in the dependency tree via other Spatie packages — meaning the marginal cost is close to zero. It is deliberately not a filesystem abstraction: it does one thing, so it will not compete with Storage or Flysystem or drag you into a rewrite.
What we are explicitly not claiming: this will not reduce infrastructure spend on its own, is not a substitute for object storage, and does not solve capacity planning. It removes a class of self-inflicted bugs and one recurring design argument. That is the whole pitch, and it is enough.
How can I help you explore Laravel packages today?