本文实例讲述了php swoole异步读取、写入文件操作。分享给大家供大家参考,具体如下:

异步读取文件:

异步写入文件:

【示例】

读取文件 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,读取大文件使用 、)