覆盖数据:
$fp = fopen("file.txt", "rw+");
fseek($fp, 100000000); // move to the position
fwrite($fp, $string, 100); // Overwrite the data in this position
fclose($fp);
注入数据
这是一个棘手的问题,因为你必须rewrite
文件。它可以通过以下方式进行优化partial modification
from point of injection
而不是整个文件
$string = "###INJECT THIS DATA ##### \n";
injectData("file.txt", $string, 100000000);
使用的功能
function injectData($file, $data, $position) {
$fpFile = fopen($file, "rw+");
$fpTemp = fopen('php://temp', "rw+");
$len = stream_copy_to_stream($fpFile, $fpTemp); // make a copy
fseek($fpFile, $position); // move to the position
fseek($fpTemp, $position); // move to the position
fwrite($fpFile, $data); // Add the data
stream_copy_to_stream($fpTemp, $fpFile); // @Jack
fclose($fpFile); // close file
fclose($fpTemp); // close tmp
}