php 使用php,如何在不覆盖文本文件开头的情况下插入文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/103593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using php, how to insert text without overwriting to the beginning of a text file
提问by ConroyP
I have:
我有:
<?php
$file=fopen(date("Y-m-d").".txt","r+") or exit("Unable to open file!");
if ($_POST["lastname"] <> "")
{
fwrite($file,$_POST["lastname"]."\n");
}
fclose($file);
?>
but it overwrites the beginning of the file. How do I make it insert?
但它会覆盖文件的开头。我如何让它插入?
回答by ConroyP
I'm not entirely sure of your question - do you want to write data and not have it over-write the beginning of an existing file, or write new data to the start of an existing file, keeping the existing content after it?
我不完全确定您的问题 - 您想写入数据而不是覆盖现有文件的开头,还是将新数据写入现有文件的开头,并保留其后的现有内容?
To insert text without over-writing the beginning of the file, you'll have to open it for appending (a+rather than r+)
要在不覆盖文件开头的情况下插入文本,您必须打开它进行追加(a+而不是r+)
$file=fopen(date("Y-m-d").".txt","a+") or exit("Unable to open file!");
if ($_POST["lastname"] <> "")
{
fwrite($file,$_POST["lastname"]."\n");
}
fclose($file);
If you're trying to write to the start of the file, you'll have to read in the file contents (see file_get_contents) first, then write your new string followed by file contents to the output file.
如果您尝试写入文件的开头,则必须先读入文件内容(请参阅 参考资料file_get_contents),然后将新字符串和文件内容写入输出文件。
$old_content = file_get_contents($file);
fwrite($file, $new_content."\n".$old_content);
The above approach will work with small files, but you may run into memory limits trying to read a large file in using file_get_conents. In this case, consider using rewind($file), which sets the file position indicator for handle to the beginning of the file stream.
Note when using rewind(), not to open the file with the a(or a+) options, as:
上述方法适用于小文件,但在尝试使用file_get_conents. 在这种情况下,请考虑使用rewind($file),它将句柄的文件位置指示符设置为文件流的开头。请注意,使用rewind(), 时不要使用a(或a+)选项打开文件,如:
If you have opened the file in append ("a" or "a+") mode, any data you write to the file will always be appended, regardless of the file position.
如果您以追加(“a”或“a+”)模式打开文件,则无论文件位置如何,您写入文件的任何数据都将始终被追加。
回答by oskarth
A working example for inserting in the middle of a file stream without overwriting, and without having to load the whole thing into a variable/memory:
一个在文件流中间插入而不覆盖的工作示例,无需将整个内容加载到变量/内存中:
function finsert($handle, $string, $bufferSize = 16384) {
$insertionPoint = ftell($handle);
// Create a temp file to stream into
$tempPath = tempnam(sys_get_temp_dir(), "file-chainer");
$lastPartHandle = fopen($tempPath, "w+");
// Read in everything from the insertion point and forward
while (!feof($handle)) {
fwrite($lastPartHandle, fread($handle, $bufferSize), $bufferSize);
}
// Rewind to the insertion point
fseek($handle, $insertionPoint);
// Rewind the temporary stream
rewind($lastPartHandle);
// Write back everything starting with the string to insert
fwrite($handle, $string);
while (!feof($lastPartHandle)) {
fwrite($handle, fread($lastPartHandle, $bufferSize), $bufferSize);
}
// Close the last part handle and delete it
fclose($lastPartHandle);
unlink($tempPath);
// Re-set pointer
fseek($handle, $insertionPoint + strlen($string));
}
$handle = fopen("file.txt", "w+");
fwrite($handle, "foobar");
rewind($handle);
finsert($handle, "baz");
// File stream is now: bazfoobar
回答by SeanDowney
If you want to put your text at the beginning of the file, you'd have to read the file contents first like:
如果要将文本放在文件的开头,则必须先阅读文件内容,例如:
<?php
$file=fopen(date("Y-m-d").".txt","r+") or exit("Unable to open file!");
if ($_POST["lastname"] <> "")
{
$existingText = file_get_contents($file);
fwrite($file, $existingText . $_POST["lastname"]."\n");
}
fclose($file);
?>
回答by Eduardo Campa?ó
You get the same opening the file for appending
你同样可以打开文件进行追加
<?php
$file=fopen(date("Y-m-d").".txt","a+") or exit("Unable to open file!");
if ($_POST["lastname"] <> "")
{
fwrite($file,$_POST["lastname"]."\n");
}
fclose($file);
?>

