本文实例讲述了PHP Swoole异步读取、写入文件操作。分享给大家供大家参考,具体如下:
异步读取文件:swoole_async_readfile
异步写入文件:swoole_async_writefile
读取文件 readfile.php:
<?php
$res = swoole_async_readfile(__DIR__."/1.txt", function($filename, $content) {
echo "文件名:{$filename} 内容:{$content}\n";
});
echo "读取文件\n";
var_dump($res);
执行结果:
写入文件 writefile.php:
<?php
$content = date("Ymd H:i:s")."\n";
$res = swoole_async_writefile(__DIR__."/1.txt", $content, function($filename) {
echo "追加写入{$filename}\n";
}, FILE_APPEND);
echo "写入文件\n";
var_dump($res);
执行结果:
1.txt:
(说明:以上两个函数可读取最大文件为4M,读取大文件使用 swoole_async_read、swoole_async_write)
扫码二维码 获取免费视频学习资料

- 本文固定链接: http://phpxs.com/post/6671/
- 转载请注明:转载必须在正文中标注并保留原文链接
- 扫码: 扫上方二维码获取免费视频资料
查 看2022高级编程视频教程免费获取