0

I am using PHP to generate a text file that is used in a computer with windows 2000 OS and mozilla firefox browser(its old dont ask why please). The file is generate correctly and runes smoothly on windows 10 and latest version of fire fox.

My problem now is that the new line does not render in the old version(windows 2000 OS and firefox 10)

I have tried using the ff:

1. /n
2. /r/n
3. <br>
4. $string = "This\r\nis\n\ra\nstring\r"; echo nl2br($string);
5. PHP_EOL

All not generating the desired output. It is just appending the BR as text

enter image description here

CODE IS :

header("Content-Type: text/html; charset=UTF-8"); 
header("Content-Type: multipart/form-data; boundary=something");  
header("Content-Length: ".strlen($data).""); 
header("Content-Disposition: attachment; filename=".$_GET['name'].""); 
echo nl2br(base64_decode($data));

sample text is : "Assembly Lot Summary Report " . "\r\n" . "\r\n" ;

FYI : Notepad(only) is used to open the text file

guradio
  • 15,524
  • 4
  • 36
  • 57
  • Add minimal HTML strucure: `your text here`. Creating static file first can help. Maybe browser missdetect something. – Daniel Sęk Nov 12 '19 at 07:38
  • @DanielSęk it works in newer OS and mozilla firefox version. The new lines are added as expected. My only assumption is because the version of mozilla and OS is old – guradio Nov 12 '19 at 07:54
  • Do you want to generate plain text file for Notepad (in this case using `nl2br` does not make sense) or HTML file to open in browser (in this case `
    ` will be correctly interpreted).
    – Daniel Sęk Nov 12 '19 at 08:45
  • unfortunately requirement is to use notepad – guradio Nov 13 '19 at 01:39
  • Make sure that generated file has `\r\n` line ends. Older Notepads (for example in Windows XP) does not handle line ends with only `\n` properly. Use `text/plain` as @Jimmix has written, do not use `nl2br`. Look up with hex editor what line ends are generated. You can also manually convert line ends (depending on client machine) and send file as `application/octet-stream` (binary stream). – Daniel Sęk Nov 13 '19 at 06:25

1 Answers1

1

if you want to have new line the same way you have in text editor when you hit enter key then the header should be:

Content-Type: text/plain

instead of

Content-Type: text/html;

so the web browser exactly knows that you want to pass a text file.

You may double check if the new line is returned to the browser by just examining the page source - go to meun > edit > view source or hit CTR+U

I would also try to remove this line:

header("Content-Type: multipart/form-data; boundary=something");

and see if it helps.

for generating new line from php you can use:

echo "\n";

or

echo PHP_EOL;

it will give you new line that depends on web server operating system - so different for Linux, different for Windows.

these are incorrect in PHP: /r or /n. you need to use backslash and they only work if double quotes "\n" are used more read.

more read on text/plain

Jimmix
  • 5,644
  • 6
  • 44
  • 71
  • the reason i didnt use text is because when using
    it will just output
    im using `\n`. still no avail
    – guradio Nov 12 '19 at 06:55