PHPackages                             zenstruck/temp-file - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. zenstruck/temp-file

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

zenstruck/temp-file
===================

Temporary file wrapper.

v1.3.0(6mo ago)291.3k↑71.6%22MITPHPPHP &gt;=8.1CI passing

Since Nov 5Pushed 4mo ago2 watchersCompare

[ Source](https://github.com/zenstruck/temp-file)[ Packagist](https://packagist.org/packages/zenstruck/temp-file)[ Docs](https://github.com/zenstruck/temp-file)[ GitHub Sponsors](https://github.com/kbond)[ GitHub Sponsors](https://github.com/nikophil)[ RSS](/packages/zenstruck-temp-file/feed)WikiDiscussions 1.x Synced 2d ago

READMEChangelog (6)Dependencies (5)Versions (7)Used By (2)

zenstruck/temp-file
===================

[](#zenstrucktemp-file)

[![CI Status](https://github.com/zenstruck/temp-file/workflows/CI/badge.svg)](https://github.com/zenstruck/temp-file/actions?query=workflow%3ACI)[![codecov](https://camo.githubusercontent.com/cf42c2482304e7eca6d49f5d0370041d593f22ddc10bdcf833ed54dbf0d78ef6/68747470733a2f2f636f6465636f762e696f2f67682f7a656e73747275636b2f74656d702d66696c652f6272616e63682f312e782f67726170682f62616467652e7376673f746f6b656e3d58354f5959514b523742)](https://codecov.io/gh/zenstruck/temp-file)

Temporary file wrapper. Created files are deleted at the end of the script.

Installation
------------

[](#installation)

```
composer require zenstruck/temp-file
```

API
---

[](#api)

### Create `TempFile`'s

[](#create-tempfiles)

```
use Zenstruck\TempFile;

// create empty file with random name (in /tmp)
$file = TempFile::new();

// create empty file with random filename, with extension (in /tmp)
$file = TempFile::withExtension('txt');

// create file with specific filename (in /tmp)
$file = TempFile::withName('my-file.txt'); // creates empty file
$file = TempFile::withName('my-file.txt', 'some content'); // creates file with string content
$file = TempFile::withName('my-file.txt', \fopen('some/file.txt', 'r')); // creates file with resource as content
$file = TempFile::withName('my-file.txt', new \SplFileInfo('some/file.txt')); // creates file from existing file (existing file is copied)

// create for existing file
$file = TempFile::new('some/file.txt'); // note: will be deleted at the end of the script

// create with string content
$file = TempFile::for('some contents');

// create with resource
$file = TempFile::for(\fopen('some/file.txt', 'r'));

// create from another file (existing file is copied)
$file = TempFile::for(new \SplFileInfo('some/file.txt'));

// create image
$image = TempFile::image(); // temporary 10x10 image with 'jpg' as the extension
$image = TempFile::image(100, 50); // customize the dimensions
$image = TempFile::image(type: 'gif'); // customize the image type
$image = TempFile::image(name: 'my-image.png'); // customize the file name
```

### Using `TempFile`'s

[](#using-tempfiles)

```
/** @var \Zenstruck\TempFile $file */

$file->contents(); // string - the file's contents
$file->refresh(); // self - clearstatcache() on the file (refreshes metadata)
$file->delete(); // self - delete the file

// is instance of \SplFileInfo
$file->getMTime(); // int - last modified timestamp
$file->getExtension(); // string - file extension
```

### Long-Running Processes

[](#long-running-processes)

Created `TempFile`'s are automatically deleted at the end of the script by keeping track of created files and purging with `register_shutdown_function`. If using a long-running PHP process (like a worker or Swoole/RoadRunner runtime) the files will not be purged until the process is stopped. This creates a memory leak as the tracked created files grows in memory. To combat this, you'll need to hook into some kind of event in your process that enables you to clear these type of leaks and call `TempFile::purge()` manually.

Symfony Integration
-------------------

[](#symfony-integration)

A simple service is provided to purge `TempFile`'s at the end of each request and, if using symfony/messenger, after a job is processed.

To use, register the service:

```
# config/packages/zenstruck_temp_file.yaml

services:
    Zenstruck\TempFile\Bridge\Symfony\PurgeTempFiles:
        autoconfigure: true
```

###  Health Score

49

—

FairBetter than 94% of packages

Maintenance71

Regular maintenance activity

Popularity36

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 95.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~199 days

Recently: every ~288 days

Total

7

Last Release

138d ago

PHP version history (2 changes)v1.0.0PHP &gt;=8.0

v1.3.0PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/707369cc916e0ea1aacbf077dcba464f611cef879f024d8944311a54a15224b3?d=identicon)[kbond](/maintainers/kbond)

---

Top Contributors

[![kbond](https://avatars.githubusercontent.com/u/127811?v=4)](https://github.com/kbond "kbond (21 commits)")[![1ed](https://avatars.githubusercontent.com/u/162986?v=4)](https://github.com/1ed "1ed (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/zenstruck-temp-file/health.svg)

```
[![Health](https://phpackages.com/badges/zenstruck-temp-file/health.svg)](https://phpackages.com/packages/zenstruck-temp-file)
```

###  Alternatives

[thecsea/simple-caldav-client

A simple and complete php caldav client

2172.6k3](/packages/thecsea-simple-caldav-client)[tecnocen/yii2-bootstrap-year-calendar

Yii2 widget for bootstrap-year-calendar plugin

1318.0k](/packages/tecnocen-yii2-bootstrap-year-calendar)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
