承接 philiprehberger/safe-file-writer 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

philiprehberger/safe-file-writer

Composer 安装命令:

composer require philiprehberger/safe-file-writer

包简介

Atomic file writes with temp-file swap and file locking

README 文档

README

Tests Latest Version on Packagist Last updated

Atomic file writes with temp-file swap and file locking.

Requirements

  • PHP 8.2+

Installation

composer require philiprehberger/safe-file-writer

Usage

Write a file atomically

use PhilipRehberger\SafeFileWriter\SafeFile;

// Writes to a temp file first, then renames — atomic on POSIX systems
SafeFile::write('/path/to/config.txt', 'key=value');

// Parent directories are created automatically
SafeFile::write('/path/to/deep/nested/file.txt', 'content');

Write JSON

SafeFile::writeJson('/path/to/data.json', [
    'name' => 'Philip',
    'scores' => [10, 20, 30],
]);
// Writes pretty-printed JSON with a trailing newline

// Custom JSON flags
SafeFile::writeJson('/path/to/compact.json', $data, JSON_UNESCAPED_UNICODE);

Append to a file

// Appends with exclusive lock; creates file if it doesn't exist
SafeFile::append('/var/log/app.log', "[2026-03-13] Info: started\n");
SafeFile::append('/var/log/app.log', "[2026-03-13] Info: finished\n");

Read a file

// Reads with a shared lock
$content = SafeFile::read('/path/to/config.txt');

Read JSON

$data = SafeFile::readJson('/path/to/data.json');
// Returns decoded array/object

Check existence

if (SafeFile::exists('/path/to/file.txt')) {
    // ...
}

Delete a file

$deleted = SafeFile::delete('/path/to/file.txt');
// Returns true if deleted, false if file didn't exist

Backup Before Write

use PhilipRehberger\SafeFileWriter\SafeFile;

// Backs up existing file with timestamp, then writes new content atomically
SafeFile::writeWithBackup('/path/to/config.txt', 'new config');
// Creates /path/to/config.txt.2026-03-31T120000.bak before overwriting

// Store backups in a custom directory
SafeFile::writeWithBackup('/path/to/config.txt', 'updated', '/var/backups');

// Same for JSON files
SafeFile::writeJsonWithBackup('/path/to/data.json', ['version' => 2]);

File Checksums

use PhilipRehberger\SafeFileWriter\Checksum;
use PhilipRehberger\SafeFileWriter\SafeFile;

// Compute a SHA-256 checksum (default)
$hash = SafeFile::checksum('/path/to/file.txt');

// Use a different algorithm
$md5 = SafeFile::checksum('/path/to/file.txt', 'md5');

// Verify a file against a known checksum
$valid = SafeFile::verifyChecksum('/path/to/file.txt', $expectedHash);

// Compare two files
$same = Checksum::compareFiles('/path/to/a.txt', '/path/to/b.txt');

Atomic Multi-File Writes

use PhilipRehberger\SafeFileWriter\SafeFile;

// Write multiple files atomically — all or nothing
SafeFile::writeMany([
    '/path/to/config.json' => '{"key": "value"}',
    '/path/to/settings.yaml' => 'debug: true',
    '/path/to/version.txt' => '1.1.0',
]);
// If any file fails to write, previously written files are rolled back

API

Method Description Returns
SafeFile::write(string $path, string $content) Atomic write via temp-file + rename void
SafeFile::writeJson(string $path, mixed $data, int $flags = ...) Atomic JSON write void
SafeFile::writeWithBackup(string $path, string $content, ?string $backupDir = null) Backup existing file, then atomic write void
SafeFile::writeJsonWithBackup(string $path, mixed $data, ?string $backupDir = null) Backup existing file, then atomic JSON write void
SafeFile::writeMany(array $files) Atomic multi-file write with rollback void
SafeFile::append(string $path, string $content) Append with exclusive lock void
SafeFile::read(string $path) Read with shared lock string
SafeFile::readJson(string $path) Read and decode JSON mixed
SafeFile::checksum(string $path, string $algo = 'sha256') Compute file checksum string
SafeFile::verifyChecksum(string $path, string $expected, string $algo = 'sha256') Verify file against expected checksum bool
SafeFile::exists(string $path) Check if file exists bool
SafeFile::delete(string $path) Delete file if it exists bool
BackupManager::backup(string $path, ?string $backupDir = null) Create timestamped backup of a file ?string
Checksum::compute(string $path, string $algo = 'sha256') Compute file hash string
Checksum::verify(string $path, string $expected, string $algo = 'sha256') Verify file hash matches expected bool
Checksum::compareFiles(string $pathA, string $pathB, string $algo = 'sha256') Compare checksums of two files bool
FileWriteException Directory creation, temp-file creation, write, or rename failure
FileReadException File not found, open failure, lock failure, or read failure
\JsonException Invalid JSON on writeJson() or readJson()

Development

composer install
vendor/bin/phpunit
vendor/bin/pint --test
vendor/bin/phpstan analyse

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

philiprehberger/safe-file-writer 适用场景与选型建议

philiprehberger/safe-file-writer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 48 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 03 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「file」 「lock」 「write」 「atomic」 「safe」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 philiprehberger/safe-file-writer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 philiprehberger/safe-file-writer 我们能提供哪些服务?
定制开发 / 二次开发

基于 philiprehberger/safe-file-writer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 48
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 37
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-13