本文主要给大家分享的是基于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)
 
        关于PHP的swoole 异步文件读写操作的介绍就到这,有需要的朋友可以参考上述方法和代码,希望本文对大家学习有帮助,更多PHP的swoole 异步相关内容,大家可以关注其他文章。

dawei

【声明】:舟山站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。