webimpress/safe-writer
Safely and atomically write files in PHP: create temp files, write/flush, then rename into place to avoid partial writes. Handles permissions, directories, and errors for reliable config/cache/log output in CLI and web apps.
Install via Composer: composer require webimpress/safe-writer. This package provides a single main class, Webimpress\SafeWriter\Writer, which safely writes content to files by first writing to a temporary file and then atomic-rename (via rename()), eliminating partial writes and race conditions. Start by using the static method Writer::writeFile($filePath, $content). For common use cases like config caching or log file rotation, this is plug-and-play — no configuration needed beyond ensuring the target directory is writable.
config.php) in production without risking partial reads during deployment or concurrent deploys.writeFile() in try-catch to gracefully handle permission or I/O failures; the method throws Webimpress\SafeWriter\Exception\IOException on failure.Writer::writeFile($path, $content, $tempDir) when the temp file must reside on the same filesystem as the target (e.g., across NFS or docker volumes).Storage disk extension or custom Laravel commands (e.g., php artisan cache:generate) to avoid partial cache writes.rename() only works atomically if temp and target paths share the same filesystem mount — if not, race conditions may reappear. Use the optional $tempDir argument to enforce this.flock()) if multiple processes write to the same file simultaneously.chmod($filePath, $mode) after writing if strict permission control is needed.writeFile().rename() will overwrite the link target, not the link itself — but verify with is_link() if relevant to your use case.$content must be held in full — consider streaming via tempnam() + stream_copy_to_stream() instead.How can I help you explore Laravel packages today?