Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active February 16, 2016 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurozumi/a7e765b32e8e08fd1279 to your computer and use it in GitHub Desktop.
Save kurozumi/a7e765b32e8e08fd1279 to your computer and use it in GitHub Desktop.
【PHP】ディレクトリ内のテキストファイルをすべて結合する方法
<?php
$it = new AppendIterator();
// globでディレクトリ内のテキストファイルを検索
foreach(glob("dir/*.txt") as $filename)
{
// ファイルオブジェクトを作成してAppendIteratorオブジェクトに追加する
$it->append(new SplFileObject($filename, "r"));
}
// 新しいファイルを作成する
$file = new SplFileObject("all.txt", "w");
// 1行ずつ読み込んでデータをファイルに追加する
foreach($it as $line)
{
$file->fwrite($line);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment