php 如何将文件添加到开头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3332262/
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
How do I prepend file to beginning?
提问by mathew
In PHP if you write to a file it will write end of that existing file.
在 PHP 中,如果您写入文件,它将写入该现有文件的结尾。
How do we prepend a file to write in the beginning of that file?
我们如何在该文件的开头添加要写入的文件?
I have tried rewind($handle)function but seems overwriting if current content is larger than existing.
我尝试过rewind($handle)函数,但如果当前内容大于现有内容,则似乎会被覆盖。
Any Ideas?
有任何想法吗?
回答by alex
$prepend = 'prepend me please';
$file = '/path/to/file';
$fileContents = file_get_contents($file);
file_put_contents($file, $prepend . $fileContents);
回答by Fraxtil
The file_get_contents solution is inefficient for large files. This solution may take longer, depending on the amount of data that needs to be prepended (more is actually better), but it won't eat up memory.
file_get_contents 解决方案对于大文件效率低下。此解决方案可能需要更长的时间,具体取决于需要预先添加的数据量(实际上越多越好),但它不会占用内存。
<?php
$cache_new = "Prepend this"; // this gets prepended
$file = "file.dat"; // the file to which $cache_new gets prepended
$handle = fopen($file, "r+");
$len = strlen($cache_new);
$final_len = filesize($file) + $len;
$cache_old = fread($handle, $len);
rewind($handle);
$i = 1;
while (ftell($handle) < $final_len) {
fwrite($handle, $cache_new);
$cache_new = $cache_old;
$cache_old = fread($handle, $len);
fseek($handle, $i * $len);
$i++;
}
?>
回答by misima
$filename = "log.txt";
$file_to_read = @fopen($filename, "r");
$old_text = @fread($file_to_read, 1024); // max 1024
@fclose(file_to_read);
$file_to_write = fopen($filename, "w");
fwrite($file_to_write, "new text".$old_text);
回答by deceze
Another (rough) suggestion:
另一个(粗略)建议:
$tempFile = tempnam('/tmp/dir');
$fhandle = fopen($tempFile, 'w');
fwrite($fhandle, 'string to prepend');
$oldFhandle = fopen('/path/to/file', 'r');
while (($buffer = fread($oldFhandle, 10000)) !== false) {
fwrite($fhandle, $buffer);
}
fclose($fhandle);
fclose($oldFhandle);
rename($tempFile, '/path/to/file');
This has the drawback of using a temporary file, but is otherwise pretty efficient.
这有使用临时文件的缺点,但在其他方面非常有效。
回答by dockeryZ
When using fopen() you can set the mode to set the pointer (ie. the begginng or end.
使用 fopen() 时,您可以设置模式以设置指针(即开始或结束。
$afile = fopen("file.txt", "r+");
'r' Open for reading only; place the file pointer at the beginning of the file.
'r+' Open for reading and writing; place the file pointer at the beginning of the file.
'r' 只读;将文件指针放在文件的开头。
'r+' 开放读写;将文件指针放在文件的开头。
回答by Dillon Burnett
$file = fopen('filepath.txt', 'r+') or die('Error');
$txt = "/n".$string;
fwrite($file, $txt);
fclose($file);
This will add a blank line in the text file, so next time you write to it you replace the blank line. with a blank line and your string.
这将在文本文件中添加一个空行,因此下次写入时替换空行。用一个空行和你的字符串。
This is the only and best trick.
这是唯一也是最好的把戏。

