betterphp does not currently provide support for this item.

14 comments found.

Hello, wow first comment… just to let you know you might want to fix your strip_tags() on the demo… it doesn’t seem to be working

that’s because i forgot my test server has magic quotes enabled…

thanks for telling me

can I also point out, that you are not buying a comments system, so breaking the page and telling me I need to do more work on it is not that helpful.

I kinda like this idea. Just out of curiousity…are the methods accessing the file separately each time, or is it kinda like a DB where you’re constantly connected?

Just trying to see if you’ve tested this out on large amounts of data. I can see a huge benefit versus a database as it relates to backup, as I could just backup flatfiles versus having to restore a database…

Definitely. I’ve always been a advocate of grabbing data and manipulating it server-side anyway. A relevant suggestion might be to create some cool “select” methods, to really compete with database queries. Especially stuff like grouping, min/max, etc. Just a thought….

Data File
10000 Inserts: 0.02156400680542
10000 Selects: 0.0066959857940674

10000 Inserts: 0.90946888923645
10000 Selects: 0.042078018188477

interesting results…

that’s also quite a good idea, a select() method could be added very easily allowing these options, ill defiantly look into that.

thinking about it, you could store an array of data and they use the php array_* functions to do most of the things that mysql allows.

Well yeah, that’s where I was going with it. It would just be a really cool bonus for people to save them even more time. That’s the main thing I see that a normal database would have over your server-side solution. Purchasing…

I guess that would make it easy to implement too,

thanks for purchasing, hope you like it.

hello a little question

why use in data_file class on line 21 double array_pop ??

array_pop($this->syspath); array_pop($this->syspath);

sorry this question i’m not so the hero in PHP . And sorry for my english.

to remove the last two elements from the array, one being empty and one being the current folder.

take that section of code and put it into a new file and experiment with commenting one array_pop and you should see the difference.

unfortunately array_pop will not take an argument of how many elements to remove from the end of the array so it has to be called twice, that got on my nerves at the time I wrote it too.

The whole point of that section of code is to give me a full base path to work from, to make it more portable,

So is this just an abstraction or does this do anything more?

it does everything described in the documentation,

because the system works mostly with arrays, and they are so easy to manipulate with PHP anyway I left it at the documented methods.

what further functionality are you looking for ?, I’m always open to suggestions and ideas for improvement.

Great idea – from what i can gather a class/framework to deal with flatfile db manipulation, but to me the screen shot is ambigious with the mysql code and db connection details this looks like it should be for sql not flatfile. I would love to see something which is simple and can replicate the sorting methods etc of mysql in flatfile environment. To me I dislike sql for all but the most complex sites with much information. I think flatfile dbs are convenient and can simplify things a great deal which is good.

the sql example shows how you could store settings in a file for a mysql connection.

The reason I didn’t implement the select methods similar to sql, is that the system works with arrays which are quite easy to manipulate with php anyway, however this has been suggested a few times now so I will look into it.

are there any specific sql-like method you can think of,

at the moment im thinking of adding a select() function that will do some query like tasks, but id like to hear some other ideas.

hi, I prefer flatfile database over mysql. I have tried many file database before, but my opinion is that they are all right till you have a small amount of data like your test.

Data File 10000 Inserts: 0.02156400680542 10000 Selects: 0.0066959857940674

MySQL 10000 Inserts: 0.90946888923645 10000 Selects: 0.042078018188477

What if you have , lets say 100000 selects? The file can be xx MB by then. Is it gonna be still faster than mysql?


it should always be faster, when you first open the file all of the data is transferred to a php array (in memory) so SELECTS are basically getting a key from that array, very speedy :)

Its probably not a good idea to store huge amounts in the file, because you will run out of memory available to php eventually, that’s why I recommend you split the data into as many files as possible.

If i have this: Array ( [0] => Array ( [name] => Jacek [email] => [msg] => testing message )

[1] => Array
        [name] => Joe
        [email] =>
        [msg] => second testing message
[2] => Array
        [name] => Joe
        [email] =>
        [msg] => second testing message again !


How can i remove, lets say, the whole [1] (Joe’s comment)? I want to be able to remove some comments, how can i do that? I dont want to remove some key of one comment, but the whole one. Can you help me please?

you can just do


Thanks for you answer.

I did that, but i found this problem: i cant now the index number of the message i want to delete. Because now my first message its not one and i do not know how to obtain by programming that number.

Lets say now i have

[3] => Array ( [name] => Joe [email] => [msg] => second testing message )


[5] => Array ( [name] => Joe [email] => [msg] => second testing message )

How can i obtain the index number of the message i want to delete?

when you loop over the array you can do

foreach ($array as $index => $post){
   echo $post['email'];

inside that loop $index will hold the value of the current array element being used, so you could use that to add a delete link to your posts

foreach ($array as $index => $post){
   echo '<a href="?rm=', $index, '">Delete</a>';

then you can check for the $_GET[‘rm’] variable and remove the key its related to. Obviously you would have to make this available to only admins in some way, and stop it being used to just remove all of the posts.

Is that any help ?

Yes!! You are the man!!

Thank you.

5 stars for you :)

ps: How can i give you a 5 star rating? I read its needed 3 votes, how can i vote?

You know, I have no idea haha. I never actually bought anything from this site. It doesn’t seem that obvious.

Your intention to give a good rating will do ;)

hello, I’m in the need of a flat file db for a small game I’m making. I would like to store and commit the score and some basic user data(name,website,fav quote,selected avatar), as I have no php knowledge I was wondering if you have in mind adding more examples to the docs as they are now (for folks like me) too thin…


I wasnt planning on it, but you can store pretty muchy anything, the user info example could be

$user_db = new data_file('user_info.dat');

// to store user info.
$user_db->add($username, array(/* an array of data to store */));

// to get it back

// this will return the array that was stored previously.

If you are only making one change you can call commit() after add(), if you are making lots it should be called after all of them.

thanks for trying, I think i’ll stick with localStorage as js is more my thing.

Whats different about this php class than say flat file storage?

To explain what I mean…

If I used data.dat to store comments for example, and used a separator character or line break (instead of using a mysql database) to split the comments up… what difference is this to the data file script?

Will this script perform better on a large file than the above method?


The script uses a flat file method like you describe, although it uses a more robust storage method. It just provides a convenient way to manipulate the file.

i thought that was the case :)

Its good to clarify that in case anyone thinks its just the flat file method that is easily achieved.


If I had a text file with the contents: Title: Hello world Text: This is some text for hello world that may go on two lines Date: 15/11/2012

Will that put those contents into an array title => Hello world, Text => This is some.... etc? I will be purchasing if it does…...

Unfortunately not, it uses it’s own data format that is not really anything like that.

I was wondering if the was sqlite and if not was there a reason to not use that?