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

yipeiwu_com6年前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中substr()函数参数说明及用法实例

本文实例讲述了php中substr()函数参数说明及用法。分享给大家供大家参考。具体如下: string substr(string $string ,int $start [, int...

PHP 防恶意刷新实现代码

复制代码 代码如下:<?php session_start(); $k=$_GET['k']; $t=$_GET['t']; $allowTime = 1800;//防刷新时间 $...

php 带逗号千位符数字的处理方法

通常用number_format(); 来格式化数字,默认情况千位符是用逗号间隔的,比如: 复制代码 代码如下: echo number_format("10000.01231", 2)...

php实现的mongoDB单例模式操作类

本文实例讲述了php实现的mongoDB单例模式操作类。分享给大家供大家参考,具体如下: 看了好多mongo类都不尽人意。最后发现根本不需要自己封装类。php mongo 的扩展自带的方...

php抓取https的内容的代码

直接用file_get_contents,会报错; 复制代码 代码如下: $url = (https://xxx.com"); file_get_contents($url); 错误:...