View previous topic :: View next topic |
Author |
Message |
cmxflash -
Joined: 11 Dec 2004 Posts: 872
|
Posted: Sat Oct 22, 2005 12:20 pm Post subject: Flatfile-database |
|
|
I've been working on a small website, that uses one flatfile-database.
I use this script to make PHP read the db.txt-file:
Code: | $open = fopen("db.txt", "r");
$file=fgets($open,1024);
fclose($open);
if ($file == ""){
echo "<b>Error.</b><hr>";
} else {
echo $file;
} |
This script will only output the first line of the database. How do I make it output everything in the database? |
|
Back to top |
|
|
MonkeyNation -
Joined: 05 Feb 2005 Posts: 921 Location: Cardiff
|
Posted: Sat Oct 22, 2005 1:47 pm Post subject: |
|
|
Code: | /* Probably don't need the explode. Can't be arsed to put it into theory. */
foreach(explode("\n", implode("", file("db.txt"))) as $this_key => $this_value) {
echo $this_value;
} |
Or something, probably. Don't use file wrappers all that much, MySQL 4 lyfe. _________________
|
|
Back to top |
|
|
TRUSTAbyss -
Joined: 29 Oct 2003 Posts: 3752 Location: USA, GA
|
Posted: Sat Oct 22, 2005 10:38 pm Post subject: |
|
|
This will loop through the entire db.txt file and output the results. If the
file is empty , I could guess that you want an Error to be printed. LateR!
Note: This will also go to Error , if db.txt does not exist.
Code: |
<?php
if (@filesize("db.txt") != 0) {
$open = fopen("db.txt", "r");
while (!feof($open)) {
$file = fgets($open, 1024);
echo $file;
}
fclose($open);
}else{
echo "<b>Error.</b><hr>";
}
?>
|
Note: If you have a huge file , the file() function would open that entire
file all at once and can slow down performance , this code uses a buffer.
Sincerely , TRUSTpunk
Last edited by TRUSTAbyss on Sat Oct 22, 2005 11:54 pm; edited 16 times in total |
|
Back to top |
|
|
roganty -
Joined: 08 Jun 2004 Posts: 357 Location: Bristol, UK
|
Posted: Sat Oct 22, 2005 10:44 pm Post subject: Re: Flatfile-database |
|
|
the file() function reads the file into an array
Code: | $open = fopen("db.txt", "r");
$fileArray=file($open);
fclose($open);
if(! is_array($fileArray) ){
echo "<b>Error.</b><hr>";
}else{
foreach( $fileArray as $val )
print "<br />$val";
} |
_________________ Anthony R
Roganty | Links-Links.co.uk |
|
Back to top |
|
|
TRUSTAbyss -
Joined: 29 Oct 2003 Posts: 3752 Location: USA, GA
|
Posted: Sat Oct 22, 2005 11:16 pm Post subject: |
|
|
I edited my last post to show you why the file() function can be a bad
option to use. Reading a file into chunks can give better performance.
Sincerely , TRUSTpunk
Last edited by TRUSTAbyss on Sun Oct 23, 2005 12:00 am; edited 3 times in total |
|
Back to top |
|
|
MonkeyNation -
Joined: 05 Feb 2005 Posts: 921 Location: Cardiff
|
Posted: Sat Oct 22, 2005 11:42 pm Post subject: Re: Flatfile-database |
|
|
roganty wrote: | the file() function reads the file into an array
Code: | $open = fopen("db.txt", "r");
$fileArray=file($open);
fclose($open);
if(! is_array($fileArray) ){
echo "<b>Error.</b><hr>";
}else{
foreach( $fileArray as $val )
print "<br />$val";
} |
|
Kinda what I did, but you don't need to use fopen etc, you can just file().
TRUSTpunk -> I'd speculate that it probably isn't, but if it is I doubt it would be that much of a difference. _________________
|
|
Back to top |
|
|
cmxflash -
Joined: 11 Dec 2004 Posts: 872
|
Posted: Sun Oct 23, 2005 12:47 am Post subject: |
|
|
Correct me if I'm wrong, but this should read the file and output it exactly as it is, right?
Code: |
$n = file_get_contents("file.txt");
echo $n;
|
|
|
Back to top |
|
|
|