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

时间:2022-11-06 23:14:44

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

异步读取文件swoole_async_readfile

异步写入文件:swoole_async_writefile

【示例】

读取文件 readfile.php:

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

执行结果:

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

写入文件 writefile.php:

?
1
2
3
4
5
6
7
8
<?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);

执行结果:

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

1.txt:

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

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

希望本文所述对大家PHP程序设计有所帮助。

原文链接:https://blog.csdn.net/msllws/article/details/84794121