Jump to content

Write into a file without erasing previous recordings


g_p_java

Recommended Posts

Good evening,

 

I'm using the following code in order to write in a file :

 


$newfile="/home/newfile.txt";

$file = fopen ($newfile, "w");

//while() //I would like to place in a while, and EOF condition, but i don't know how to do that

fwrite($file, $ip);
fwrite($file, $newString);
fclose ($file);

 

 

as i see in many fwrite examples, usually they write oonly one thing in a file.

1. I would like to write an a) IP and a b )String. The above code works, but it doesn't places any space between the IP and the string e.g. 1.2.3.4How are you? (No space between them)

2. Also, with fwrite the previous recordings get erased by the new ones.

What shall i do for that? Shall i place an EOF in a while or shall i use another function?

 

Thanks  :)

$newfile="/home/newfile.txt";

$file = fopen ($newfile, "a");

$output = $ip." ".$newString;

fwrite($file, $output);

fclose ($file);

 

That will open the file in append mode and write the data at the end of existing data in the file.

 

Not sure why you would need the while, maybe you could clarify

 

$newfile="/home/newfile.txt";

$file = fopen ($newfile, "a");

$output = $ip." ".$newString;

fwrite($file, $output);

fclose ($file);

 

That will open the file in append mode and write the data at the end of existing data in the file.

 

Not sure why you would need the while, maybe you could clarify

 

 

Thanks, actually it worked!

It writes at the EOF, but how am i supposed to add a '\n' for every recording?

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.