[PHP] 物件化方式的讀檔
一般讀檔的方式
物件化讀檔的方式
這樣感覺 code 有比較好看一些 XD
Ref:
https://stackoverflow.com/questions/13246597/how-to-read-a-large-file-line-by-line
https://www.php.net/manual/en/splfileobject.fwrite.php
https://www.php.net/manual/en/class.splfileobject.php
$handler = fopen("file.csv");
while (($line = fgets($handler)) !== false) {
// 執行相關邏輯
fwrite($handler, "test\r\n");
}
fclose($handler);
物件化讀檔的方式
$filepath = "test.tsv";
$file = new SplFileObject($filepath);
while (!$file->eof()) {
// 執行相關邏輯
$line = rtrim($file->fgets());
$file->fwrite("12345");
}
$file = null;
這樣感覺 code 有比較好看一些 XD
Ref:
https://stackoverflow.com/questions/13246597/how-to-read-a-large-file-line-by-line
https://www.php.net/manual/en/splfileobject.fwrite.php
https://www.php.net/manual/en/class.splfileobject.php
留言
張貼留言