Swoole Coroutine System: readFile

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


Latest version: pecl install openswoole-22.1.2

Declaration

<?php Swoole\Coroutine\System::readFile(string $filename): string|false

Parameters

filename

The location of the file to read. Use an absolute filepath.

Return

Returns string content as a whole when the file read is successful. When the file cannot be read or there is an error, false is returned. Use swoole_last_error() to get error information.

Description

Read the whole contents of a file.

There is no size limit to this method, the content of the file will be read and stored into memory, large files will cause more memory to be used but this method is non-blocking to other workers or processes.

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


Example

<?php

Co\run(function() use ($filename)
{
  $filename = __DIR__ . "/test.txt";
  $r =  Co\System::readFile($filename);
  var_dump($r);
});
Last updated on August 31, 2022