私のウェブサイトの一部のコンテンツを更新するために、約95,000行の209MB .txtファイルが週に一度自動的にサーバーにプッシュされます。問題は、そのような大きなファイルを処理するのに十分なメモリを割り当てることができないことです。そのため、大きなファイルをそれぞれ5,000行の小さなファイルに分割したいのです。大きなファイルをPHPで多数の小さなファイルに分割する
ファイルが小さい部分に分割されるまで、私はfile()を使用できません。そのため、SplFileObjectを使用しています。しかし私はそれをどこにも持っていません。ここに私が達成したいものの擬似コードがあります:
read the file contents
while there are still lines left to be read in the file
create a new file
write the next 5000 lines to this file
close this file
for each file created
run mysql update queries with the new content
delete all of the files that were created
ファイルはcsv形式です。
EDIT:
function getLine($number) {
global $handle, $index;
$offset = $index[$number];
fseek($handle, $offset);
return explode("|",fgets($handle));
}
$handle = @fopen("content.txt", "r");
while (false !== ($line = fgets($handle))) {
$index[] = ftell($handle);
}
print_r(getLine(18437));
fclose($handle);
どのような処理をおこなっていますか? 'fopen'と' fgets'を介した読み込みは、すべてを配列に格納しようとしていない限りうまくいきます。 – mfonda
'fgets'をSecondingする。そうすれば、ファイル全体をメモリにロードせずに1行ずつ読むことができます。 – Fanis
私はfgetsを使ってファイルをバイト単位で出力できます。ファイルの内容を行番号で取得するためにfgetsを使用できますか? – Jarred