Why does this PHP code overwrite the contents of the file on opening?

I wrote some pretty complex PHP applications using this function, so I'm not sure what is going on here.

I am currently writing an application that uses online history to save content (via ajax get). The API I wrote to store user history in a file is very simple:

$myFile = "./snhistory/".$_GET["uid"];
$stringData = urldecode($_GET["name"])."\n";
$file = fopen($myFile,"a");
fwrite($file,$stringData);
fclose($file);

It looks like code that adds the data found in name, plus a new line to the end of the file, right? Well, that’s not how PHP sees it. It adds one name, then when I run the code with a different name, it overwrites the first and only the second appears. I tried everything I could think:

 file_put_contents($myFile,file_get_contents($myFile).$stringData);

and

 file_put_contents($myFile,$stringData,FILE_APPEND);

AND

fopen($myFile,"w");
fseek($file,0,SEEK_END);

. - PHP, - ? , , , , . , .

, .

: , , ', - URL . .

+5
2

:

<?php
$myFile = "./myfile.txt";
$myString="hello world\n";
$fp = fopen($myFile, "a");
fwrite($fp, $myString);
fclose($fp);
?>

( , php app.php, "hello world" .

, . , ...

php. , , - , , - .. - -, , ?

, , , (URL-) ($_GET("name")) , "" - "" . , , - , , .

+1

, , . type , LF, CRLF, . - , Notepad ++.

Linux, \n \r. cat . , , .

0

All Articles