use Swoole\Coroutine;
use function Swoole\Coroutine\run;
// 一键协程化,包括fopen,fread/fgets,fwrite/fputs,file_get_contents、file_put_contents,unlink,mkdir,rmdir
Coroutine::set(['hook_flags'=> SWOOLE_HOOK_FILE]);
run(function() {
$path = './1.txt';
$fp = fopen($path, 'a+');
// 协程写入,返回写入长度,失败返回false
var_dump('写入1:'.fwrite($fp, '123321'.PHP_EOL));
// 协程写入,返回写入长度,失败返回false
var_dump('写入2:'.fputs($fp, '123321'.PHP_EOL));
// 协程读取指定$size的数据,失败返回false
var_dump('读取1:'.fread($fp, filesize($path)));
while ($val = fgets($fp)) {
var_dump('读取2:'.$val . PHP_EOL);
}
var_dump(file_get_contents($path));
file_put_contents($path, '1233211234567', FILE_APPEND);
});