Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Using 'list' to list out variables from .txt file

9:36 am on May 9, 2011 (gmt 0)

New User

5+ Year Member

joined:May 9, 2011
votes: 0

So I have created a flatfile script where I store text data into a .txt file-format database instead of using MySQL. I'm planning on listing out the contents of that .txt file, which is formatted to appear like so:

1\¦Lines of text here\¦IP address\¦
2\¦Lines of text here\¦IP address\¦

The first 'segment' is the ID, as in the numerical order of the text stored. The second part is where the text information is submitted via user input and then stored into the .txt database. The third part is the user's IP address.

So then my list function goes something like this:

list($id, $data, $ip) = getRow(DATABASE)

It lists out the information from my .txt file (DATABASE) using this function:

function getRow($file) { $fd = fopen($file, "r") or die("Can not open file: $file"); $rstr = fread($fd, filesize($file)); fclose($fd); return explode("\¦", $rstr); }

Now the list and function thing works fine because I'm able to echo out individual 'segments' separately. Say if I want to echo out the user submitted data, then I just go and the corrrect info appears. But then the problem is, I can't echo out every single line in the database. If I do the echo data thing, I can only echo out the data from the first row (ID no.1). This probably has something to do with the way the function is coded but I'm not that familiar with PHP yet so I don't know how to tweak so it can echo out the data from every single row.

Does anyone know what I can modify to make it work?
2:19 am on May 17, 2011 (gmt 0)

Full Member

10+ Year Member

joined:June 1, 2007
votes: 0

I think you may want to read in a larger amount of the file but you could possible run into issue if the file is very large try this file_get_contents [php.net...]
5:45 pm on May 17, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
votes: 0

Both fopen and file_get_contents read in the whole file (the first in an array, the second in a string.) So when you explode using fread, you're only storing the first three exploded elements.

You will need to either step through the array or incorporate fgets into your function to read it line by line, or fseek to seek the line pointer you want. This code will likely be incorrect, typing on the fly, but something like

list($id, $data, $ip) = getRow(DATABASE,1234);


list($id, $data, $ip) = getRow(DATABASE);

function getRow($file,$myid=null) {
$fd = fopen($file, "r") or die("Can not open file: $file");
if ($fd) {
while (($buffer = fgets($fd, 4096)) !== false) {
list($id,$desc,$ip) = explode($buffer);
if ($myid and ($myid==$id)) {
return array($id,$desc,$ip);
return array($id,$desc,$ip);

The down side is this is not very efficient to use as a "get row" for printing out a whole file, it would read the whole file up to the matching $id for each row, thus reading it as many times as you have lines. (In fact, it would be kind of piggish . . .) I'd write a different function for that one that only reads the file once.