Page 1 of 1

Displaying contents of txt file using PHP

Posted: Thu Apr 07, 2011 4:18 am
by implications
I'm building a flat file script by using txt files as the database. The user inputs data into a form and the data is stored in the .txt file as lines of text. Now I want to display the data in the .txt file using PHP but I'm not sure how I'm supposed to do in order to rearrange the contents of the .txt file and display it on a webpage.

Because the data that's been submitted is currently stored like this in the .txt file: Line 1, Line 2, Line 3. When I display it on the webpage (I'm just using PHP includes to include the .txt file), it looks exactly as it does in the .txt file, but I want it to be able to manipulate the data so it looks something like this:

Line 1
Line 2
Line 3

How would I be able to achieve something like this? I've just started meddling with PHP so I'm not sure where I'm supposed to start.

Re: Displaying contents of txt file using PHP

Posted: Thu Apr 07, 2011 5:14 am
by social_experiment
Take a look at fopen : fopen

Re: Displaying contents of txt file using PHP

Posted: Thu Apr 07, 2011 8:34 am
by implications
I've looked through the fopen manual but it's mainly for displaying the .txt file. It doesn't say anything about manipulating the text in the .txt file before displaying it?

Re: Displaying contents of txt file using PHP

Posted: Thu Apr 07, 2011 9:30 am
by social_experiment

Code: Select all

<?php
 $filename = 'test.txt';
 $fp = fopen( $filename, 'r') or die('Couldnt open ' . $filename);
 while (!feof($fp)) {
  $line = fgets($fp, 1024);
  echo $line . '<br />';
 }
?>
Hth

Re: Displaying contents of txt file using PHP

Posted: Sat Apr 09, 2011 12:05 am
by implications
I've tried that but it doesn't work because the contents of the .txt file are kind of stuck together. It's displayed like this: FirstlinehereSecondlinehereThirdlinehere. So when you try to display it, it's impossible to break up each phrase into a different line.

Re: Displaying contents of txt file using PHP

Posted: Sat Apr 09, 2011 3:50 am
by social_experiment
Try making the 1024 value a bit smaller. Is there anyway to change the way that the values are stored in the text file, add a space, a comma?

Re: Displaying contents of txt file using PHP

Posted: Sat Apr 09, 2011 5:07 am
by implications
Changing it to a smaller value does not seem to make much of a difference.

I think it's possible to add a space or comma but I don't know how to change the way the data is entered into the .txt file because the data is submitted through a submit form and then written to the .txt file using this function:

Code: Select all

function doWrite($textfile, $data, $type) {
	$file = fopen($textfile, $type) or die("File could be opened.");
	if (flock($file, LOCK_EX)) {
		fwrite($file, $data);
		flock($file, LOCK_UN);
	} else {
		exit("File could not be opened..");
	}
	fclose($file);
}
When the data is written onto the .txt file, all the lines are stuck together. How would I modify my current code to write the submitted data with some sort of separator?

Re: Displaying contents of txt file using PHP

Posted: Sat Apr 09, 2011 11:29 am
by social_experiment

Code: Select all

<?php
fwrite($file, $data." ");
?>
Edit : That should place a space between each value

Re: Displaying contents of txt file using PHP

Posted: Sat Apr 09, 2011 11:56 am
by greyhoundcode
Could the problem be that you are echoing out the contents of the text file in a browser? If so the new lines/carriage returns will be treated like any other whitespace.

nl2br() function

I don't know if that is the problem you are experiencing, but if so the above function could help.

Re: Displaying contents of txt file using PHP

Posted: Sun Apr 10, 2011 2:37 am
by social_experiment

Code: Select all

function doWrite($textfile, $data, $type) {
	$file = fopen($textfile, $type) or die("File could be opened.");
	if (flock($file, LOCK_EX)) {
		fwrite($file, $data. "\r\n");
		flock($file, LOCK_UN);
	} else {
		exit("File could not be opened..");
	}
	fclose($file);
}

function doRead($textfile, $type) {
	$array = file_get_contents($textfile);
	return explode("\r\n", $array);	
}
I found a way to add a new line after each value. The function below that will return an array containing the contents of the file.