Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 25.x
Latest version:
pecl install openswoole-25.2.0
<?php Swoole\Coroutine\System::readFile(string $filename): string|false
The location of the file to read. Use an absolute filepath.
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.
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.
<?php
Co\run(function() use ($filename)
{
$filename = __DIR__ . "/test.txt";
$r = Co\System::readFile($filename);
var_dump($r);
});