Displaying contents of txt file using PHP
Moderator: General Moderators
-
implications
- Forum Commoner
- Posts: 25
- Joined: Thu Apr 07, 2011 3:59 am
Displaying contents of txt file using PHP
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.
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.
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Displaying contents of txt file using PHP
Take a look at fopen : fopen
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
-
implications
- Forum Commoner
- Posts: 25
- Joined: Thu Apr 07, 2011 3:59 am
Re: Displaying contents of txt file using PHP
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?
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Displaying contents of txt file using PHP
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 />';
}
?>“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
-
implications
- Forum Commoner
- Posts: 25
- Joined: Thu Apr 07, 2011 3:59 am
Re: Displaying contents of txt file using PHP
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.
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Displaying contents of txt file using PHP
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?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
-
implications
- Forum Commoner
- Posts: 25
- Joined: Thu Apr 07, 2011 3:59 am
Re: Displaying contents of txt file using PHP
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:
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?
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);
}- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Displaying contents of txt file using PHP
Code: Select all
<?php
fwrite($file, $data." ");
?>“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
- greyhoundcode
- Forum Regular
- Posts: 613
- Joined: Mon Feb 11, 2008 4:22 am
Re: Displaying contents of txt file using PHP
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.
nl2br() function
I don't know if that is the problem you are experiencing, but if so the above function could help.
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Displaying contents of txt file using PHP
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);
}
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering