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);