PHPackages                             nette/safe-stream - 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. [File &amp; Storage](/categories/file-storage)
4. /
5. nette/safe-stream

ActiveLibrary[File &amp; Storage](/categories/file-storage)

nette/safe-stream
=================

Nette SafeStream: provides isolation for thread safe manipulation with files via native PHP functions.

v3.0.4(2mo ago)1174.9M—3.6%1420BSD-3-ClausePHPPHP 8.0 - 8.5CI passing

Since May 25Pushed 2mo ago29 watchersCompare

[ Source](https://github.com/nette/safe-stream)[ Packagist](https://packagist.org/packages/nette/safe-stream)[ Docs](https://nette.org)[ RSS](/packages/nette-safe-stream/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (20)Used By (20)

SafeStream: Safety for Files
============================

[](#safestream-safety-for-files)

[![Downloads this Month](https://camo.githubusercontent.com/6d172c50e771b49bc0dd6261992a906da75f8cf2f95ea390dfd243bb7f48e7d8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f6e657474652f736166652d73747265616d2e737667)](https://packagist.org/packages/nette/safe-stream)[![Tests](https://github.com/nette/safe-stream/workflows/Tests/badge.svg?branch=master)](https://github.com/nette/safe-stream/actions)[![Coverage Status](https://camo.githubusercontent.com/3f75bdd8e9e31f60110c3d0be52c32f13b5922b98909ed93a029a6101bf47ff1/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6e657474652f736166652d73747265616d2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/nette/safe-stream?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/92fec13780d276ba1bda1992a090d804f5de63dc3828decade5ee9130e8deb25/68747470733a2f2f706f7365722e707567782e6f72672f6e657474652f736166652d73747265616d2f762f737461626c65)](https://github.com/nette/safe-stream/releases)[![License](https://camo.githubusercontent.com/fa7d5fcf2c84b580327af52da95dd751703af65f079dc3c5a0081beac0789718/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4e65772532304253442d626c75652e737667)](https://github.com/nette/safe-stream/blob/master/license.md)

Introduction
------------

[](#introduction)

SafeStream guarantees that every read and write to a file is isolated. This means that no thread will start reading a file that is not yet fully written, or multiple threads will not overwrite the same file.

Installation:

```
composer require nette/safe-stream
```

What is it good for?
--------------------

[](#what-is-it-good-for)

What are isolated operations actually good for? Let's start with a simple example that repeatedly writes to a file and then reads the same string from it:

```
$s = str_repeat('Long String', 10000);

$counter = 1000;
while ($counter--) {
	file_put_contents('file', $s); // write it
	$readed = file_get_contents('file'); // read it
	if ($s !== $readed) { // check it
		echo 'strings are different!';
	}
}
```

It may seem that `echo 'strings differ!'` can never occur. The opposite is true. Try running this script in two browser tabs at the same time. The error will occur almost immediately.

One of the tabs will read the file at a time when the other hasn't had a chance to write it all, so the content will not be complete.

Therefore, the code is not safe if it is executed multiple times at the same time (i.e. in multiple threads). Which is not uncommon on the internet, often a server is responding to a large number of users at one time. So ensuring that your application works reliably even when executed in multiple threads (thread-safe) is very important. Otherwise, data will be lost and hard-to-detect errors will occur.

But as you can see, PHP's native file read and write functions are not isolated and atomic.

How to use SafeStream?
----------------------

[](#how-to-use-safestream)

SafeStream creates a secure protocol to read and write files in isolation using standard PHP functions. All you need to do is to specify `nette.safe://` before the file name:

```
file_put_contents('nette.safe://file', $s);
$s = file_get_contents('nette.safe://file');
```

SafeStream ensures that at most one thread can write to the file at a time. The other threads are waiting in the queue. If no thread is writing, any number of threads can read the file in parallel.

All common PHP functions can be used with the protocol, for example:

```
// 'r' means open read-only
$handle = fopen('nette.safe://file.txt', 'r');

$ini = parse_ini_file('nette.safe://translations.neon');
```

---

Documentation can be found on the [website](https://doc.nette.org/safe-stream). If you like it, **[please make a donation now](https://github.com/sponsors/dg)**. Thank you!

###  Health Score

72

—

ExcellentBetter than 100% of packages

Maintenance85

Actively maintained with recent releases

Popularity58

Moderate usage in the ecosystem

Community41

Growing community involvement

Maturity89

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 93.8% 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 ~238 days

Recently: every ~283 days

Total

19

Last Release

85d ago

Major Versions

v2.5.1 → v3.0.02022-12-13

v2.5.x-dev → v3.0.12023-08-05

PHP version history (6 changes)v2.2.0PHP &gt;=5.3.1

v2.4.0PHP &gt;=7.1

v3.0.0PHP &gt;=8.0 &lt;8.3

v3.0.1PHP 8.0 - 8.3

v3.0.2PHP 8.0 - 8.4

v3.0.3PHP 8.0 - 8.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/17f266513a3ca97500ec3d85d562b9279c7a6346358fe2b8d90390ece717a027?d=identicon)[david@grudl.com](/maintainers/david@grudl.com)

---

Top Contributors

[![dg](https://avatars.githubusercontent.com/u/194960?v=4)](https://github.com/dg "dg (121 commits)")[![fprochazka](https://avatars.githubusercontent.com/u/158625?v=4)](https://github.com/fprochazka "fprochazka (1 commits)")[![kamil-zacek](https://avatars.githubusercontent.com/u/17525718?v=4)](https://github.com/kamil-zacek "kamil-zacek (1 commits)")[![kratkyzobak](https://avatars.githubusercontent.com/u/20162433?v=4)](https://github.com/kratkyzobak "kratkyzobak (1 commits)")[![milo](https://avatars.githubusercontent.com/u/439140?v=4)](https://github.com/milo "milo (1 commits)")[![mishak87](https://avatars.githubusercontent.com/u/276500?v=4)](https://github.com/mishak87 "mishak87 (1 commits)")[![OskarStark](https://avatars.githubusercontent.com/u/995707?v=4)](https://github.com/OskarStark "OskarStark (1 commits)")[![besanek](https://avatars.githubusercontent.com/u/3389310?v=4)](https://github.com/besanek "besanek (1 commits)")[![salatos](https://avatars.githubusercontent.com/u/2593603?v=4)](https://github.com/salatos "salatos (1 commits)")

---

Tags

atomicityfilesystemisolationmultiple-threadsnettenette-frameworkphpfilesystemnettesafeisolationatomicthread safe

###  Code Quality

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/nette-safe-stream/health.svg)

```
[![Health](https://phpackages.com/badges/nette-safe-stream/health.svg)](https://phpackages.com/packages/nette-safe-stream)
```

###  Alternatives

[league/flysystem

File storage abstraction for PHP

13.6k639.1M2.2k](/packages/league-flysystem)[league/flysystem-aws-s3-v3

AWS S3 filesystem adapter for Flysystem.

1.6k263.6M790](/packages/league-flysystem-aws-s3-v3)[knplabs/gaufrette

PHP library that provides a filesystem abstraction layer

2.5k39.8M123](/packages/knplabs-gaufrette)[knplabs/knp-gaufrette-bundle

Allows to easily use the Gaufrette library in a Symfony project

72428.6M91](/packages/knplabs-knp-gaufrette-bundle)[league/flysystem-local

Local filesystem adapter for Flysystem.

225231.8M39](/packages/league-flysystem-local)[league/flysystem-bundle

Symfony bundle integrating Flysystem into Symfony applications

40129.5M87](/packages/league-flysystem-bundle)

PHPackages © 2026

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