OpenSwoole Coroutine System: writeFile

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\Coroutine\System::writeFile(string $filename, string $fileContent, int $flags): bool

Parameters

filename

The absolute filepath where to write contents to. Including any file extension if needed. The file will be created if it does not exist, make sure you have the correct permissions.

content

The content to be written to the file as a string. The maximum writeable content is 4M.

flags

The default flag is to clear the current contents and overwrite the file if it exists. But you can use FILE_APPEND to append contents to the end of the file.

Return

Returns true when successful and false when not.

Description

Write string content into a file.

This method will immediately return false if an error occurs.

Note: Consider using coroutine hooks instead so you can easily use native PHP read and write functions.


Example

<?php

co::run(function()
{
  $filename = __DIR__ . "/test.txt";
  $result = OpenSwoole\Coroutine\System::writeFile($filename, "Hello World - from OpenSwoole!\n");
  var_dump($result);
});
Last updated on September 21, 2022