异步读取文件:swoole_async_readfile文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17675.html
异步写入文件:swoole_async_writefile文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17675.html
【示例】文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17675.html
读取文件 readfile.php:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17675.html
- <?php
- $res = swoole_async_readfile(__DIR__."/1.txt",function($filename,$content) {
- echo "文件名:{$filename} 内容:{$content}\n";
- });
- echo "读取文件\n";
- var_dump($res);
执行结果:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17675.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17675.html
写入文件 writefile.php:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17675.html
- <?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);
执行结果:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17675.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17675.html
1.txt:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17675.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17675.html
(说明:以上两个函数可读取最大文件为4M,读取大文件使用 swoole_async_read、swoole_async_write)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/17675.html