ソースファイルの一部と追加するものを入れる一時ファイルが必要になります。
$sp = fopen('source', 'r');
$op = fopen('tempfile', 'w');
while (!feof($sp)) {
$buffer = fread($sp, 512); // use a buffer of 512 bytes
fwrite($op, $buffer);
}
// append new data
fwrite($op, $new_data);
// close handles
fclose($op);
fclose($sp);
// make temporary file the new source
rename('tempfile', 'source');
そうすれば、 の内容全体source
がメモリに読み込まれません。cURL を使用する場合、設定CURLOPT_RETURNTRANSFER
を省略し、代わりに一時ファイルに書き込む出力バッファーを追加できます。
function write_temp($buffer) {
global $handle;
fwrite($handle, $buffer);
return ''; // return EMPTY string, so nothing's internally buffered
}
$handle = fopen('tempfile', 'w');
ob_start('write_temp');
$curl_handle = curl_init('http://example.com/');
curl_setopt($curl_handle, CURLOPT_BUFFERSIZE, 512);
curl_exec($curl_handle);
ob_end_clean();
fclose($handle);
私はいつも明白なことを見逃しているようです。Marc が指摘したCURLOPT_FILE
ように、応答をディスクに直接書き込む必要があります。