0

I get some data from a variable, and send it via XHR to PHP file which write it in to a file. How can I make a line break in the file after every write to it?

var e='Hello world!'
$.getJSON('http://example.com/write.php?callback=&data='+e);

write.php:

$data = $_GET['data'];
if(!$op=fopen('my file name','ab')){
  $res='b';
  exit;
}
if(fwrite($op,$data)===FALSE){
  $res='b';
  exit;
}
fclose($op);
Alexander
  • 23,432
  • 11
  • 63
  • 73
Aleksov
  • 1,200
  • 5
  • 17
  • 27

3 Answers3

2

Try writing or appending a PHP_EOL to the file after you have written your data.

fwrite($op, PHP_EOL);
Alexander
  • 23,432
  • 11
  • 63
  • 73
1

Try:

$data = $_GET['data'];

if(!$op=fopen('my file name','ab')){
  $res='b';
  exit;
}
if(fwrite($op,$data . PHP_EOL)===FALSE){
  $res='b';
  exit;
}
fclose($op);
mallix
  • 1,399
  • 1
  • 21
  • 44
0

AFAIK, fwrite() will automatically append a newline for each write.

Are you running PHP on a Windows server? You might try to open the file to write to in 'at' mode (append, translate line-endings) in stead of 'binary' mode (ab);

$op = fopen('my file name','at');

http://php.net/manual/en/function.fopen.php

thaJeztah
  • 27,738
  • 9
  • 73
  • 92