Member Avatar for Member #710047

I want to add text string at the end of the text file. I used "a+" for it but it delete all the content and than enters text to it. So please help.

Dani AI

Generated

@kirtan_thakkar the problem is that you open a handle with fopen("a+"), but then you call file_put_contents() without the append flag. file_put_contents() opens its own handle and, by default, truncates the file before writing. That is why your content disappears. Either stick with the fopen/fwrite path, or use file_put_contents() correctly, but not both in the same write.

If you do not need to read from the file, avoid "a+"; appending does not require read access. Also, watch your handle names; in ’s first snippet the variable mismatch ($f vs $fh) would cause issues even if the mode were right.

Here is a compact, safe append helper that adds a newline and prevents interleaved writes when multiple requests run at once:

<?php
function append_line(string $path, string $line): void {
    $bytes = file_put_contents($path, $line . PHP_EOL, FILE_APPEND | LOCK_EX);
    if ($bytes === false) {
        throw new RuntimeException("Failed to write to {$path}");
    }
}
append_line('status.txt', $data);

Notes:

  • FILE_APPEND makes it an append, not an overwrite (echoing ’s point).
  • LOCK_EX gives a simple advisory lock so concurrent writes do not scramble lines.
  • If you truly need to read and then write in one request, open once for reading, then append after you are done reading (or seek to end) to avoid moving the pointer mid-write.

For reference on modes and locking details, see the PHP manual pages for fopen modes and flock.

Recommended Answers

All 9 Replies

<script>
var text1 = "abc";
var text2 = "def";

document.write(text1 + text2);
</script>
Member Avatar for Member #710047

I want to add a text at the end of a file and code should be in php. Not in java.

I'm sorry. Was browsing both forums. Do you have your fopen/fwrite codes?

Just use dot (.)

<?php
 
  $text1 = "World";

  echo "Hello ".$text1; //The output will be Hello World

  $text2="Hello";

  echo $text2." World"; //The output will be Hello World
?>

I think he's writing into an external file. "a+" should be correct. So I hope he can provide us with the codes to debug.

$f = fopen( 'filename.txt', 'a' );
fwrite( $f, 'something' );
fclose( $f );
Member Avatar for Member #710047

Sorry guys.. I was unable to reply.. Now here is my code..

$file = "status.txt";
$f = fopen($file,"a+");
file_put_contents($file,$data);
fclose($f);

But it is removig all content of the file and than adding new contenet..

Hi,
Replace the 3rd line with the below code.

file_put_contents($file,$data,FILE_APPEND);

The third parameter need to be set in file_put_contents to append with existing data. otherwise it would overwrite the existing file contents. check the manual file-put-contents

Wrong code:

    $file = "status.txt";
    $f = fopen($file,"a+");
    // file_put_contents($file,$data); // This is doing ur file blank & then add the data
    fwrite($fh, $data); // This will append the string to the end of of the file
    fclose($f);



Working code


$myFile = "testing.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$string = "New String 1\n";
fwrite($fh, $string);
$string = "New String 2\n";
fwrite($fh, $string);
fclose($fh);

Prashant Patil

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.