Using php how to insert text without overwriting at the beginning of a text file

I have:

<?php $file=fopen(date("Ymd").".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 to insert it?

+7
source share
4 answers

I'm not quite sure about your question - do you want to write data and not overwrite the beginning of an existing file or write new data to the beginning of an existing file, preserving the existing content after that?

To insert text without overwriting the beginning of the file , you will have to open it to add ( a+ not r+ )

 $file=fopen(date("Ymd").".txt","a+") or exit("Unable to open file!"); if ($_POST["lastname"] <> "") { fwrite($file,$_POST["lastname"]."\n"); } fclose($file); 

If you are trying to write to the beginning of the file , you will need to read the contents of the file (see file_get_contents ), then enter a new line and then the contents of the file into the output file.

 $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 limitations when trying to read a large file using file_get_conents . In this case, consider using rewind($file) , which sets the file position indicator for the handle to the top of the file stream. Please note that when using rewind() do not open the file with the parameters a (or a+ ), for example:

If you opened the file in append mode ("a" or "a +"), any data that you write to the file will always be added regardless of the position of the file.

+25
source

A working example for inserting in the middle of a file stream without overwriting and without having to load it all 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 

Composer lib for it can be found here

+1
source

If you want to put your text at the top of the file, you first need to read the contents of the file, for example:

 <?php $file=fopen(date("Ymd").".txt","r+") or exit("Unable to open file!"); if ($_POST["lastname"] <> "") { $existingText = file_get_contents($file); fwrite($file, $existingText . $_POST["lastname"]."\n"); } fclose($file); ?> 
0
source

You will get the same as the file to add

 <?php $file=fopen(date("Ymd").".txt","a+") or exit("Unable to open file!"); if ($_POST["lastname"] <> "") { fwrite($file,$_POST["lastname"]."\n"); } fclose($file); ?> 
0
source

Source: https://habr.com/ru/post/1276943/


All Articles