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

Temp File Laravel Package

zenstruck/temp-file

A lightweight PHP library for creating temporary files and directories with automatic cleanup. It offers safe, predictable helpers for tests, uploads, and intermediate processing, reducing leftover files and simplifying lifecycle management.

Deep Wiki
Context7

zenstruck/temp-file is a lightweight PHP utility for creating temporary files and directories that clean themselves up automatically. It provides a small, expressive API for working with throwaway data in tests, scripts, or any code that needs safe scratch space without leaving artifacts behind.

  • Create temporary files with optional initial content
  • Create temporary directories for grouped scratch data
  • Automatic cleanup when objects are destroyed
  • Simple access to file paths and read/write helpers
  • Framework-agnostic and Composer-ready
Frequently asked questions about Temp File
What is zenstruck/temp-file used for in PHP applications?
zenstruck/temp-file provides an object-oriented wrapper for creating temporary files that clean themselves up automatically. It is useful when you need a real file path for exports, debug dumps, test fixtures, or intermediate processing without manually tracking unlink calls. The package also offers configurable prefixes and helpers for writing content and dumping debug information.
How do I install zenstruck/temp-file in a Laravel app?
Install it with Composer using composer require zenstruck/temp-file. Because it is a framework-agnostic PHP package, you typically do not need to register a Laravel service provider or publish configuration. You can use the package classes directly inside your Laravel application code.
Does zenstruck/temp-file support Laravel 10 or Laravel 11?
zenstruck/temp-file is not Laravel-specific, so it does not declare support for individual Laravel versions. In practice, your Laravel version just needs to run a PHP version that satisfies the package Composer constraints. Check the package requirements on Packagist before adding it to an older Laravel project.
How does automatic temporary file cleanup work?
The package creates temp file objects that delete their underlying file automatically when the object is destroyed or during shutdown. This reduces the need for manual unlink calls in your application code. You should still avoid relying on automatic cleanup alone for sensitive data or long-running processes.
Can I configure a prefix for temporary filenames?
Yes, the package supports configurable prefixes for generated temporary files. A recognizable prefix makes it easier to identify files created by your application or test suite. This is helpful when auditing the system temp directory or troubleshooting leftover files.
How do I write content to a temporary file with zenstruck/temp-file?
The package includes helpers for writing content to the temp file and dumping debug information. You can create the temp file object, populate it through the provided API, and then use its path with other tools or processes. Check the README for the exact method names supported by your installed version.
Is zenstruck/temp-file safe to use in PHPUnit or Laravel tests?
Yes, it is useful in tests where you need a real file path but want cleanup handled automatically. Be mindful that cleanup happens when the object is destroyed or at shutdown, so assert the file contents while the temp file object is still in scope. This helps avoid confusing test failures caused by premature cleanup.
What happens to temp files if PHP crashes or exits unexpectedly?
Automatic deletion on destruct and shutdown covers normal object lifecycle and normal script shutdown. A hard crash, kill -9, or fatal error before shutdown handlers run can leave temporary files behind. In production, pair the package with a periodic temp directory cleanup policy for defense in depth.
What are alternatives to zenstruck/temp-file?
Native PHP functions such as tempnam and tmpfile are the baseline for temporary file handling. Framework filesystem helpers or packages like spatie/temporary-directory offer different ergonomics and directory-level cleanup. zenstruck/temp-file is a lightweight option focused on object-oriented temp files and automatic deletion.
Is zenstruck/temp-file actively maintained?
Zenstruck maintains a set of PHP packages, but you should check Packagist and the GitHub repository for current release activity and compatibility. Because this package is small and framework-agnostic, it has a narrow maintenance surface. Review the latest tagged release before relying on it in a long-term Laravel project.
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.
diephp/sequences
users-au/socialite-provider
twidpay/twid-logger
mrnewport/laravel-plaid
mrnewport/laravel-repo
mrnewport/laravel-docsign
mrnewport/laravel-stow
mrnewport/laravel-flow
mrnewport/laravel-priceable
mrnewport/laravel-nestedterms
fredmeng/laravel-apidoc-generator
itstructure/laravel-multilingual-tools
pdustdar/elasticsearch
markupai/php-sdk
rlacerda83/lumem-microservice-shipment
uni-method/symfony-lib
tacticmedia/scim-server-bundle
neika-ai/symfony-bundle
mattleads/agent-publishing-bundle
glavweb/framework-bundle