您需要一个临时文件,您可以在其中放入源文件的一部分加上要附加的内容:
$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');
这样,源的所有内容都不会被读入内存.使用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直接写入磁盘响应.