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

yipeiwu_com5年前PHP代码库

本文实例讲述了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_readswoole_async_write

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP扩展开发教程》、《PHP网络编程技巧总结》、《php curl用法总结》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》及《php字符串(string)用法总结

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

相关文章

php实现递归与无限分类的方法

本文实例讲述了php实现递归与无限分类的方法,分享给大家供大家参考。具体实现方法如下: <?php echo "<pre>"; $area = array...

浅谈PHP表单提交(POST&GET&URL编/解码)

POST方法不依赖于URL,不会将传递的参数值显示在地址栏中。另外,POST方法可以没有限制地传递数据到服务器,所有提交的信息在后台传输,用户在浏览器是看不到这一过程的,安全性高。 PO...

PHP实现限制IP访问的方法

本文实例讲述了PHP实现限制IP访问的方法。分享给大家供大家参考,具体如下: //获取客户端ip if (getenv("HTTP_CLIENT_IP")) $ip = gete...

PHP中strpos、strstr和stripos、stristr函数分析

本文为大家分析了 PHP中strpos、strstr和stripos、stristr函数,供大家参考,具体内容如下 strpos mixed strpos ( string $hayst...

php微信高级接口群发 多客服

本文实例为大家分享了php微信高级接口群发、多客服源码,供大家参考,具体内容如下 /** * 微信接口调用 * 依赖 * 全局变量 * global $uid 公众号用户id...