Using PHP, How to Insert Text Without Overwriting to the Beginning of a Text File

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

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+)

$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.

$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:

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.

Adding code to .txt file without overwriting it

"Now it doesn't work at all"

^--« As per your edit, this will write each entry on a new line:

I changed:

$content = stripslashes($_POST['content']);

to

$content = stripslashes($_POST['content'] . "\n");

Using \n will have all new entries on a new line.

PHP

<?php

$fn = "content.txt";

if (isset($_POST['content'])){

$content = stripslashes($_POST['content'] . "\n");

$fp = fopen($fn,"a+") or die ("Error opening file in write mode!");

fputs($fp,$content);

fclose($fp) or die ("Error closing file!");

}

?>

<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post">

<textarea rows="25" cols="40" name="content"></textarea>

<input type="submit" value="Submit">

</form>

Written to file:

a1

a2

a3

a4

a5

Hello world!!

Testing 123

Written to file one at a time:

a1,a2,a3,a4,a5

Entered in the textarea box as shown and written to file as shown:

Hello world!!

Testing 123


If you want the data to be entered starting from the top, use:

<?php

if (isset($_POST['content'])){

$content = stripslashes($_POST['content'] . "\n");

$file_data = $content;
$file_data .= file_get_contents('content.txt');
file_put_contents('content.txt', $file_data);

}

?>

<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post">

<textarea rows="25" cols="40" name="content"></textarea>

<input type="submit" value="Submit">

</form>

How to write data to a file with file_put_contents() without overwriting it?

If the file already exists, by default file_put_contents() would overwrite the existing file.

To prevent the current file from being overwritten, pass the FILE_APPEND flag as the function's third argument.

For example:

if (file_exists($file_name)) {
file_put_contents($file_name, $some_value, FILE_APPEND);
} else {
//...
}

Inserting a string into the middle of a TXT file with PHP

Explode the file by new line.

That will give you a nice array with each line.

Loop through and find the point of insertion and add the text you want to add.

Then implode it back and write it to the file back.

Add contents of 1 file to the top of another file without overwrite in PHP?

I think the best way to go about this for such small strings is to read and store the contents of the file5.txt, and then append the read content to fullnews.txt.

$content = file_get_contents("file5.txt");
file_put_contents("fullnews.txt", $content, FILE_APPEND);


Related Topics



Leave a reply



Submit