I am working with large text files in php (1GB+), I am using
file_get_contents(\"file.txt\", NULL, NULL, 100000000,100);
To get data from
A variant on Baba's answer, not sure if it would be more efficient when working with larger files:
function injectData($file, $data, $position) {
$fpFile = fopen($file, "rw+");
$fpTemp = fopen('php://temp', "rw+");
stream_copy_to_stream($fpFile, $fpTemp, $position);
fwrite($fpTemp, $data);
stream_copy_to_stream($fpFile, $fpTemp, -1, $position);
rewind($fpFile);
rewind($fpTemp);
stream_copy_to_stream($fpTemp, $fpFile);
fclose($fpFile);
fclose($fpTemp);
}
injectData('testFile.txt', 'JKL', 3);
Variant of my earlier method that eliminates one of the stream_copy_to_stream() calls, so should be a shade faster:
function injectData3($file, $data, $position) {
$fpFile = fopen($file, "rw+");
$fpTemp = fopen('php://temp', "rw+");
stream_copy_to_stream($fpFile, $fpTemp, -1, $position);
fseek($fpFile, $position);
fwrite($fpFile, $data);
rewind($fpTemp);
stream_copy_to_stream($fpTemp, $fpFile);
fclose($fpFile);
fclose($fpTemp);
}