PHP Swoole异步读取、写入文件操作示例

异步读取文件:swoole_async_readfile

异步写入文件:swoole_async_writefile

【示例】

读取文件 readfile.php:

  1. <?php
  2. $res = swoole_async_readfile(__DIR__."/1.txt",function($filename,$content) {
  3. echo "文件名:{$filename} 内容:{$content}\n";
  4. });
  5. echo "读取文件\n";
  6. var_dump($res);

执行结果:

PHP Swoole异步读取、写入文件操作示例

写入文件 writefile.php:

  1. <?php
  2. $content = date("Ymd H:i:s")."\n";
  3. $res = swoole_async_writefile(__DIR__."/1.txt",$content,function($filename) {
  4. echo "追加写入{$filename}\n";
  5. },FILE_APPEND);
  6.  
  7. echo "写入文件\n";
  8. var_dump($res);

执行结果:

PHP Swoole异步读取、写入文件操作示例

1.txt:

PHP Swoole异步读取、写入文件操作示例

(说明:以上两个函数可读取最大文件为4M,读取大文件使用 swoole_async_readswoole_async_write

THE END