ファイル全体を読むにはさまざまな方法がありますが、ストリームを使用して必要なデータだけを読み込むほうがよいでしょう。
<?php
$source="file.txt";
$destination="file2.txt";
$requiredLines=10000;
//compare the modification times, if source is newer than destination - then we do our work
if(filemtime($source)>filemtime($destination)){
//work out maximum length of file, as one line may be the whole file.
$filesize = filesize($source);
//open file for reading - this doesnt actually read the file it allows us to "stream" it
$sourceHandle = fopen($source, "r");
//open file for writing
$destinationHandle = fopen($destination, "w");
$linecount=0;
//loop through file until we reach the end of the file (feof) or we reach the desired number of lines
while (!feof($sourceHandle) && $linecount++<$requiredLines) {
//read one line
$line = stream_get_line($sourceHandle, $filesize, "\n");
//write the line
fwrite($destinationHandle,$line);
}
//close both files
fclose($sourceHandle);
fclose($destinationHandle);
}
あなたがここにストリーム上でより多くの情報を見つけることができます:Understanding PHP Streams
ファイルを()配列を作成し、配列はカウンターで、 – nogad
は他への書き込み、1から行を読んで、ループを使用してインデックスされます –
ジェネレータを使用する方がよいでしょうhttp://php.net/manual/en/language.generators.overview.php – bxN5