Swoole Coroutine System: writeFile

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

Declaration

<?php Swoole\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 = Co\System::writeFile($filename, "Hello World - from Swoole!\n");
  var_dump($result);
});
Last updated on August 31, 2022