通知短信+运营短信,5秒速达,支持群发助手一键发送🚀高效触达和通知客户 广告
# swoole_async_IO --- [TOC=2,3] --- ## **1.swoole_async_readfile** 功能:异步读取文件内容<br> 函数原型:<br> ```php swoole_async_readfile(string $filename, mixed $callback); ``` 参数说明:<br> | 参数 | 描述 | | -------- | ----- | | filename | 待读取的文件名 | | callback | 读取结束的回调函数 | 回调函数原型:<br> ```php function callback($filename, $content); ``` 回调函数参数说明:<br> | 参数 | 描述 | | -------- | ----- | | filename | 文件名 | | content | 读取到的文件内容 | 说明:<br> swoole_async_readfile会将文件内容全部复制到内存,所以不能用于大文件的读取<br> 如果要读取超大文件,请使用[swoole_async_read](#3swoole_async_read)函数<br> swoole_async_readfile最大可读取**4M**的文件,受限于**SW_AIO_MAX_FILESIZE**宏<br> 示例:<br> ```php swoole_async_readfile( __DIR__."/Test.txt", function($filename, $content) { echo "$filename: $content"; }); ``` ## **2.swoole_async_writefile** 功能:异步写文件<br> 函数原型:<br> ```php swoole_async_writefile(string $filename, string $content, mixed $callback); ``` 参数说明:<br> | 参数 | 描述 | | -------- | ----- | | filename | 待写入的文件名 | | content | 待写入的文件内容 | | callback | 写入结束的回调函数 | 回调函数原型:<br> ```php function callback($filename); ``` 回调函数参数说明:<br> | 参数 | 描述 | | -------- | ----- | | filename | 文件名 | 说明:<br> swoole_async_writefile最大可写入4M的文件,也可以不指定回调函数<br> 示例:<br> ```php swoole_async_writefile('test.log', "This is a test log", function($filename) { echo "wirte ok.\n"; }); ``` ## **3.swoole_async_read** 功能:异步分段读文件<br> 函数原型:<br> ```php swoole_async_read(string $filename, mixed $callback, int $trunk_size = 8192); ``` 参数说明:<br> | 参数 | 描述 | | -------- | ----- | | filename | 待读取的文件名 | | callback | 读取结束的回调函数 | | trunk_size | 每次读取的字节数 | 回调函数原型:<br> ```php function callback($filename, $content); ``` 回调函数参数说明:<br> | 参数 | 描述 | | -------- | ----- | | filename | 文件名 | | content | 读取到的文件内容,如果为空代表文件已读完。 | 说明:<br> swoole_async_read每次读取trunk_size个字节,读完后会自动调用callback函数<br> 在callback回调函数中,可以通过`return true/false`来控制是否继续读取文件。返回true代表继续读取,返回false代表停止读取。 示例:<br> ```php swoole_async_read( __DIR__."/Test.txt" , function($filename, $content){ if( empty( $content ) ) { return false; } else { echo "$filename: $content"; return true; } } , 8192 ); ``` ## **4.swoole_async_write** 功能:异步写文件<br> 函数原型:<br> ```php bool swoole_async_write(string $filename, string $content, int $offset = -1, mixed $callback = NULL); ``` 参数说明:<br> | 参数 | 描述 | | -------- | ----- | | filename | 待写入的文件名 | | content | 待写入的文件内容 | | offset | 写入文件的位置 | | callback | 写入结束的回调函数 | 回调函数原型:<br> ```php function callback($filename, $writen); ``` 回调函数参数说明:<br> | 参数 | 描述 | | -------- | ----- | | filename | 文件名 | | writen | 本次调用已经写入的字节数 | 说明:<br> swoole_async_write过传入的offset参数来确定写入的位置,当offset为-1时表示追加写入到文件的末尾<br> 示例:<br> ```php swoole_async_write( 'test_1.log', "This is a test log\n" , -1 , function( $filename, $writen ){ echo "$filename: write $writen byte\n"; }); ``` ## **5.swoole_async_dns_lookup** 功能:将域名解析为IP地址<br> 函数原型:<br> ```php bool swoole_async_dns_lookup(string $host, mixed $callback); ``` 参数说明:<br> | 参数 | 描述 | | -------- | ----- | | filename | 待查询的地址 | | callback | 查询结束的回调函数 | 回调函数原型:<br> ```php function callback($host, $ip); ``` 回调函数参数说明:<br> | 参数 | 描述 | | -------- | ----- | | filename | 文件名 | | writen | 本次调用已经写入的字节数 | 说明:<br> 当DNS查询失败时,比如域名不存在,回调函数传入的$ip为空<br> 示例:<br> ```php swoole_async_dns_lookup("www.baidu.com", function($host, $ip){ echo "{$host} : {$ip}\n"; }); ```