0

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.

6
Contributors
9
Replies
20
Views
6 Years
Discussion Span
Last Post by msnadz
-1
<script>
var text1 = "abc";
var text2 = "def";

document.write(text1 + text2);
</script>
0

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

0

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
?>

Edited by lyrico: n/a

0

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.

0
$f = fopen( 'filename.txt', 'a' );
fwrite( $f, 'something' );
fclose( $f );

Edited by smantscheff: n/a

0

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

Edited by kirtan_thakkar: n/a

1

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

0

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

Edited by pritaeas: Removed fake signature. Use the signature option in Edit Profile.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.