Jump to content

Reading multiple lines from a text file


gravereaper

Recommended Posts

$myFile = "testFile.txt";$fh = fopen($myFile, 'w') or die("can't open file");$stringData = "Bobby Bopper\n";fwrite($fh, $stringData);$stringData = "Tracy Tanner\n";fwrite($fh, $stringData);fclose($fh);

This code should simply write the two names on different lines but instead of a line break its displaying a character normally used a symbol for line break. How can i read this txt file so that character is considered as an actual line break and the two names appear on different lines?

Link to comment
Share on other sites

What are you looking at the text file in? Are you opening it in Notepad, or something else? If you open it in more then one program, do they all show the same thing? You should be able to attach the text file as an attachment to your post.

Link to comment
Share on other sites

I'am putting the codes right but there seems to be a problem.. i'am using the following codes to write to the file:

<?php$myFile = "testfile.txt";$fh = fopen($myFile, 'w');$datastring = "text 1\n";fwrite($fh,$datastring);$datastring = "text2\n";fwrite($fh,$datastring);fclose($fh);?>

and the following to read it:

<?php$myFile = "testfile.txt";$fh = fopen($myFile, 'r');$theData = fread($fh, filesize($myFile));fclose($fh);echo $theData;?>

But instead of separate lines, its displaying the file contents in the same line, thats what i was trying to figure out..

Link to comment
Share on other sites

The \r\n is for the text file only. Add a <br /> tag to the data in the file or in your echo statements to have the Browser output behave as you wish. The read function you are using reads the entire file contents. Maybe switch to a function which reads single lines at a time and add the br tag on each loop as you read / write the file contents. Check at the php site for the different functions available. fgets() looks promissing. http://us3.php.net/manual/en/function.fgets.php

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...