..according to php.net
Link to the info:http://www.php.net/manual/en/function.fwrite.php
Example:
<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
print "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (!fwrite($handle, $somecontent)) {
print "Cannot write to file ($filename)";
exit;
}
print "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
print "The file $filename is not writable";
}
?>
but this way you have to rewrite the whole file. I dont really know of a better way of doing it.
Modified for your use:
<?php
$filename = 'test.txt';
$somecontent = "\$var1 = Name1,Name1 ";
$somecontent .= "\$var2 = Name5,Name5 ";
$somecontent .= "\$var3 = Name1,Name1 ";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In this example we're opening $filename in write mode.
if (!$handle = fopen($filename, 'w')) {
print "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (!fwrite($handle, $somecontent)) {
print "Cannot write to file ($filename)";
exit;
}
print "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
print "The file $filename is not writable";
}
?>
Hope this helps.