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

时间:2022-07-27
本文章向大家介绍PHP Swoole异步读取、写入文件操作示例,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《PHP扩展开发教程》、《PHP网络编程技巧总结》、《php curl用法总结》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》及《php字符串(string)用法总结》

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