💣
file_get_contentsでメモリ落ちたら読む記事
file_get_contentsでこういうエラー出たら
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate ~700MB) in ***.php on line **
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate ~700MB) in ***.php on line **
file_get_contentsで開いたはいいけど、サイズでかすぎるよ~!メモリが足りないよ~!の状況
なんでそうなるの?
file_get_contentsは一撃で全部とってくるため。
→1行づつ開いてfwriteてもらえば安全だね が叶うのがtream_copy_to_stream
結論 tream_copy_to_streamの使い方
いつものやり方
$copyData = 'fuge_data.txt';
$fp = fopen('***.txt', 'w');
$contents = file_get_contents($copyData);
fwrite($fp, $contents);
fclose($fp);
file_get_contentsでクソデカデータを全部取得しようとした際にメモリが死亡。
解決ver
// コピー先&コピー元データをfopen
$copyData = 'fuge_data.txt';
$fp = fopen('***.txt','wb');
$datFp = fopen($copyData, 'rb');
// 1行づつfwrite
stream_copy_to_stream($datFp, $fp);
fclose($datFp);
fclose($fp);
stream_copy_to_streamは1行づつデータ取得&fwriteをしてくれてるから、メモリ不足になる心配なし
Discussion