本文實例講述了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程序設計有所幫助。
您可能感興趣的文章:- windows系統php環境安裝swoole具體步驟
- php使用Swoole實現毫秒級定時任務的方法
- php使用goto實現自動重啟swoole、reactphp、workerman服務的代碼
- PHP用swoole+websocket和redis實現web一對一聊天
- PHP之Swoole學習安裝教程