Forum Moderators: coopster

Message Too Old, No Replies

End-of-line char not working with fwrite.

         

nelsonm

1:31 am on Aug 30, 2011 (gmt 0)

10+ Year Member



hi all,

i can't figure out why the end-of-line char \n is not working when writing multiple lines to a file with fwrite. I'm developing on a windows system.

I here is the function code...

function message($location,$line,$status,$message){

$line = str_pad($line,3,'0',STR_PAD_LEFT);
$message = '['.$line.'-'.$status.']'.' - '.$message.'.';
$dberror = '['.__LINE__.'-'.'E'.']'.' - '.'Error-log table insert failed: '.mysql_error().'.';

$file = fopen('../log/error-log.txt','at'); // write errors to this flat file before database is opened or if database or tables can not be accessed.

switch ($location) {
case 'db': // write to database table error-log.
$query = mysql_query('INSERT INTO `error-log` (Message) VALUES ("'.CheckField($message).'")'); // CheckField escapes any special characters before being inserted into the database table.

if(!$query){
if($file)fwrite($file,$dberror.'\n');
if($file)fwrite($file,$message.'\n');
}
break;

case 'fl': // write to error-log flat file.
if($file)fwrite($file,$message.'\n');
break;

default:
break;
}

fclose($file);
}



I'm following the syntax and recommendations in the php manual but it still does not work.

I hope someone can tell me what i'm doing wrong.

thanks.

lostdreamer

7:29 am on Aug 30, 2011 (gmt 0)

10+ Year Member



Try using \r\n instead of just \n.
\n is only a newline (seems to work fine on windows, but wont do much good on linux) while \r\n is a carriage return followed by a newline.

Also: There are a few times where '' wont do you much good (when trying to echo special things like \n) ... You might want to try "\r\n" (note the double quotes)

penders

8:50 am on Aug 30, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



\n is only a newline (seems to work fine on windows, but wont do much good on linux)


\n (line feed, char code 10) is the line ending on linux. \r\n is Windows (char 13 + char 10).

... \n might not work on Windows (depends on what software you use to open the file - browsers will be OK, but Windows Notepad might not).

You might want to try "\r\n" (note the double quotes)


\r and \n are just the character escapes. So yes, you will need double quotes to enable variable parsing [uk2.php.net]. '\r\n' (single quotes) is just the literal string (backslash + r + backslash + n).

Or use the PHP constant PHP_EOL which contains whatever line ending the OS of the server uses. (Windows / Linux).

incrediBILL

11:41 am on Aug 30, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Mac & Linux tend to use just \n but Windows software tends to prefer \r\n

This should add to the confusion: [en.wikipedia.org...]

nelsonm

3:57 pm on Aug 30, 2011 (gmt 0)

10+ Year Member



lostdreamer & penders,

The double quotes did it - thanks. I did not know that double quotes enabled variable parsing. There's a lot to learn and remember!

Also, the 't' in the 'at' in the fopen call tells the function to translate '\n' to '\r\n' on windows systems.

thanks - on to the next problem!

penders

4:30 pm on Aug 30, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I did not know that double quotes enabled variable parsing.


My post above was perhaps a little misleading. Yes, double quoted strings enable variable parsing, but it's the fact that double quoted strings interpret a lot more escape sequences than single quoted strings which is what's important in this instance, not actually the variable parsing ability.

nelsonm

8:10 pm on Aug 30, 2011 (gmt 0)

10+ Year Member



ok, thanks!