本文实例讲述了PHP Swoole异步读取、写入文件操作。分享给大家供大家参考,具体如下:
【示例】
读取文件 readfile.php:
$res = swoole_async_readfile(__DIR__."/1.txt", function($filename, $content) {
echo "文件名:{$filename} 内容:{$content}\n";
});
echo "读取文件\n";
var_dump($res);
执行结果:
写入文件 writefile.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:
希望本文所述对大家PHP程序设计有所帮助。