1

I need to keep a 20k to 30k file register with a simple key:value per line. I need to keep it in a file , since other instance also will use it. Then I will need to find an especific key to get its value and also write a key:value in the file. I was wondering wich of the following methods are faster / better or considered as good practice.

In order to write to file, I know about three ways to do it: first:

$fh = fopen('myfile.txt', 'a') or die("can't open file");
fwrite($fh, 'key:value');
fclose($fh);

second or with file_put_contents

file_put_contents('myfile.txt','key:value',FILE_APPEND);

and third using a system call.

exec("echo key:value >> myfile.txt");

And also, in order to read a file and find a line a can do: Using file_get_contents

$filename = 'info.txt';
$contents = file_get_contents($filename);
foreach($contents as $line) {
$pos = strpos($line, $key);
}

Using file

$filename = 'info.txt';
$contents = file($filename);
foreach($contents as $line) {
$pos = strpos($line, $key);
}

And with a system call:

exec("grep $key | wc -l",$result);

1 Answers1

1

I guess you already considered using a database? Because otherwise you are reinventing the wheel. A database has all the advantages with fast-seeking and row-level locking.

If you are using a file, you have to build this by yourself.

I strongly advice to switch to some kind of database.

BTW, you don't mention if you are replacing values or just appending to the file.

Mark van Herpen
  • 347
  • 3
  • 12
  • also since its key:value, you can basically search and remove. If you then need an export, CSV export will be able to make it portable.Definitely should be looking at DB solution. All current webapp (nodejs, ruby, etc..) can read DB and reading from db are in terms of ms. – azngunit81 Apr 11 '16 at 15:12
  • 1
    This should have been a comment and not an answer as it doesn't answer the question asked. – Veve Apr 11 '16 at 15:13
  • Dear Mark, thanks for your comment. First: If the key exist , it will be replaced with new timestamp, if not it will append. Second: In my opinion If Im going to work with a single table db ... it is the same to use a file since im not doing relational queries and also should be faster. Please , let me know what do you think about this. Is there any good/technical reason to use a db instead a file? – user2977753 Apr 12 '16 at 13:22
  • Not all databases are relational databases :-) Take a look at NoSQL for instance. If you are looking for concurrent read/writes from different processes, a database is the way to go imho. There are lots of questions asked here for 'database vs file' questions. For instance: http://stackoverflow.com/questions/2356851/database-vs-flat-files – Mark van Herpen Apr 12 '16 at 14:12