Swoole Coroutine System: readFile

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

Declaration

<?php OpenSwoole\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 OpenSwoole\Util::getLastErrorCode() 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 =  OpenSwoole\Coroutine::readFile($filename);
  var_dump($r);
});
Last updated on September 21, 2022