I am currently needing some PHP code to append the variable $domain
to the file fqdn
. It needs a space between it and the previous domain for scripting reasons.
Here is the PHP that does that:
$file = file_get_contents('fqdn');
$file .= ' '.$domain;
file_put_contents('fqdn',$file);
However, this creates a new line which makes it impossible to work with script. Please help me.
Regards,
P.S. I did this at 12:30AM so I may have messed up badly.
It would be more efficient to just open the file for writing since you don't really need its current content. Also make sure you trim()
the content you insert at the end of the file
$handle = fopen("fqdn", "a");
fwrite($handle, trim(' '.$domain));
fclose($handle);
Use APPEND flag:
<?php
$sAppendedContents = ' ' . rand();
file_put_contents( 'fqdn', $sAppendedContents, FILE_APPEND | LOCK_EX );
?>