Page 1 of 1

Inserting Line Break

Posted: Thu Jun 15, 2006 11:46 pm
by tail
Here's the code I'm using:

Code: Select all

<?php
$filename = 'visitors.txt';
$screenname = $_GET ['screenname'];
if (is_writable($filename)) {
   if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
   }
   if (fwrite($handle, $screenname) === FALSE) {
       echo "Cannot write to file ($filename)";
       exit;
   }
   echo "Success, wrote ($screenname) to file ($filename)";
   fclose($handle);
} else {
   echo "The file $filename is not writable";
}
?>
But the problem is I need to have a line break after every value that get's put in the file. Any help?

Posted: Fri Jun 16, 2006 12:05 am
by feyd

Code: Select all

"\n"
// or
PHP_EOL // a constant in "later" versions of php
Note, \n must be in double quotes to be processed.

Posted: Fri Jun 16, 2006 12:11 am
by tail
Where would I put that?

Posted: Fri Jun 16, 2006 12:14 am
by feyd
they would need to be added while calling fwrite()

Posted: Fri Jun 16, 2006 12:17 am
by tail
This isn't working. Can you place it where it needs to be? That would be great.

Code: Select all

<?php
$filename = 'visitors.txt';
$screenname = $_GET ['screenname'];
if (is_writable($filename)) {
   if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
   }
   if (fwrite($handle, $screenname, "\n") === FALSE) {
       echo "Cannot write to file ($filename)";
       exit;
   }
   echo "Success, wrote ($screenname) to file ($filename)";
   fclose($handle);
} else {
   echo "The file $filename is not writable";
}
?>

Posted: Fri Jun 16, 2006 12:22 am
by printf
this...

Code: Select all

if (fwrite($handle, $screenname, "\n") === FALSE) {

should be....

Code: Select all

if (fwrite($handle, $screenname . "\n") === FALSE) {

you had a comma after the $screenname, it need to be a dot/period, if on Windows you also need to use "\r\n", instead on just "\n" for linux/unix and just "\r" for OSMac!


pif!

Posted: Fri Jun 16, 2006 12:26 am
by tail
Thank you :)