PHP script that outputs a text file to browser, but CR/LF's are getting ignored

Hi, I have a small PHP script running on a web server that calls an EXE file that creates a small text file. My PHP script then outputs the contents of this text file directly to the browser using the statement:

echo file_get_contents("regfile.txt");

That *does* output the contents to the browser okay, but it ignores the CR/LF's within the text file so the output looks like it's all one big continuous blob instead of proper CR/LF's after each line. is there a way to output to get those CR/LF's to be output as well?


P.S: I don't know much about PHP... just enough to be dangerous.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
HTML ignores cr/lfs.  But there are two ways to do it.  First is to wrap the text in <pre></pre> tags.  The second is to use the PHP 'nl2br' function.
If you are outputting text, make sure your Content-type: text/plain
When outputting the file In its entirety.

Dave's answers if the plain text isobar the of an HTML page.
shawn857Author Commented:
Thanks guys, I'll try those things... but the more I think about it now, the more I think it would be nicer if I could just pop up the source 'regfile.txt' in Notepad or something like that... instead of outputting the contents of it to the browser. Is there some way with PHP to do that?

Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Dave BaldwinFixer of ProblemsCommented:
You could force them to download the file.  But what is the point here anyway?  What is supposed to happen when the user sees the file?
shawn857Author Commented:
Well, the process goes like this Dave:

- they enter two fields of info into a small form and click Submit
- My PHP scripts runs my exe file which takes their input and generates a special code, I then want to present this code back to the user... either right on the browser, or by popping up Notepad to display it. The user then can highlight and copy the code.

I never thought of the "download" option - I'm just trying to make it as few steps for the user as possible. Is it not possible in PHP to pop Notepad up showing the contents of regfile.txt?

Dave BaldwinFixer of ProblemsCommented:
Is it not possible in PHP to pop Notepad up showing the contents of regfile.txt?
No, it's not, and part of the reason is that by the time it gets to the user's browser, PHP is already done running on the server.  If the text is small, the easiest way is this:
echo nl2br(file_get_contents("regfile.txt"));

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
Here is some code cobbled together. Presents the code in a <pre> window and provides a button to select the text.
Working sample here
Partially based on EE code window and select function obtained from here
<!doctype html>
<style type="text/css">
.code {
  background: #f9fbfd;
  border: 1px solid #d5d5d5;
  padding: 5px;
.selector {
  text-align: right;
.selector a {
  text-decoration: none;
  color: #2781b7;
<div style="width: 50%">
  <pre class="code" id="comment"><?php echo file_get_contents('test.txt');?></pre>
  <div class="selector">
  <a href="comment">Select all</a>
<script src=""></script>

<script type="text/javascript">
$(function() {
  $('.selector a').click(function(e) {
    var id = $(this).attr('href');

function selectElementContents(el) {
    if (window.getSelection && document.createRange) {
        // IE 9 and non-IE
        var range = document.createRange();
        var sel = window.getSelection();
    } else if (document.body.createTextRange) {
        // IE < 9
        var textRange = document.body.createTextRange();

Open in new window

Ray PaseurCommented:
Dave Baldwin answered the question right at the top, but that said, the design concept sounds like a "handshake" or "confirmation" that we use in a variety of applications to ensure that a client is in communication with a system.  (Paypal has an elaborate series of mating dances that accomplish this before money can change hands via email)  Perhaps the client has registered on a web site and we want to confirm their email, so we send them an email with a clickable link that activates their account.  If they click that link we know that their email address works.  If that is the sort of thing you're thinking about, this article may be able to help.
shawn857Author Commented:
Dave, I tried your suggestion:

echo nl2br(file_get_contents("regfile.txt"));

and it is EXACTLY what I need... perfect! This just leaves me with one more question, and it probably should be a separate one: If I have two users at my site at theoretically the same time, both will create a data file "regfile.txt"... but will the correct code get displayed to the proper user? Let me know if I should ask this as a separate question please....

Dave BaldwinFixer of ProblemsCommented:
I was wondering if you would notice that.  Unless you have a method to identify the correct file for the user, you will never know.  You need to call that program with a unique id that it can use to name the file for that user.
shawn857Author Commented:
Yep, after giving it some thought, I've come up for a solution for that "timing" problem Dave. Based on what the user enters for his input, I can just take some of that and incorporate that into the filename that holds his code. That should ensure that his filename is unique even if there happens to be someone else at just the same moment executing the PHP script too.

you can use an epoch timestamp (seconds since January 1 1970 GMT) to generate unique filenmaemes incorporated with the user
shawn857Author Commented:
Thanks guys... Julian your proposed solution gives a nice formatted output, but at this point, I really don't need it to be like that. I might use that code in the future though if I wish to get more fancy!

Dave BaldwinFixer of ProblemsCommented:
You're welcome, glad to help.
Julian HansenCommented:
@Shawn, you are welcome - code was provided for interest - Dave had already covered all the bases.

One thing to note though in response to this - it is possible to popup a notepad with the text.

While it is not strictly PHP that is doing this, it is PHP sending the header information to the browser to tell it to do that.

PHP Code
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename('test.txt'));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('test.txt'));

Open in new window

This will popup a file open / save dialog - open will open the file in whatever application is configured for .txt files.

Just for interest ....
shawn857Author Commented:
Thanks for that Julian, and it works quite nicely too except that the destination filename ('test.txt') winds up containing not only the generated activation code I create, but also all the HTML code that is on my webpage as well.

Julian HansenCommented:
except that the destination filename ('test.txt') winds up containing not only the generated activation code I create, but also all the HTML code that is on my webpage as well.

That code should not do that unless the file in question has that content in it - you can also use the same code to echo the content directly out - without first writing it to a file.

Take a look at this sample here
shawn857Author Commented:
Yes, it's strange isn't it? For me, the output file contains even the HTML code. I suppose with more investigating I could figure out the problem, but for now Dave's proposed solution:

echo nl2br(file_get_contents("regfile.txt"));

... is a workable and useful one for me, so I'll just go with that for now. Thanks for your contributions Julian!

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.