Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

append text at next line 2

Status
Not open for further replies.

kurie

Programmer
Jun 4, 2008
170
ZA
how do i appaned text at a new line in php.
my code is below but its appending on the same line. does anyone know how to fix this.

$myFile = "testFile.csv";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $Message.",".$value.",".$Company."<br>";
fwrite($fh, $stringData);
//$stringData = "New Stuff 2\n";
//fwrite($fh, $stringData);
fclose($fh);
 
you also might prefer to use file_put_contents.

Code:
$myFile = "testFile.csv";
file_put_contents($myFile, $Message.",".$value.",".$Company . "\r\n", FILE_APPEND);

or even better in this case, fputcsv
Code:
$fh = fopen($myFile, 'abt') or die("can't open file");
$return = fputcsv($fh, array($Message, $Value, $Company), ',', '"');
if ($return === false) die ('cannot write to the file');
fclose($fh);
 
thanks guys, both solutions worked for me.
 
although i don't know what is in the $Message etc variables, i strongly suggest that you use the fputcsv method or enquote each field in double quotes in order to conform to the standard csv. otherwise if one of the variables has a comma in it, you're screwed.
 
thanks very much jpadie, i never thought about that. Yes changes are very high that message will contain a comma.


---otherwise if one of the variables has a comma in it, you're screwed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top