So, I made a tiny (~2 KB, <100 lines) library for simple flat-file data storage.
How it works is, you define a file to be used, and then you can add/read/modify/delete objects as "keys" to/from the file.
All the code is on github, but I'll put it here too:
varDx.php
:
<?php
namespace varDX;
class cDX {
private $dataFile;
public function def($filename){
$this->dataFile = $filename;
}
public function write($varName, $varVal){
if(file_exists($this->dataFile)){
$foundLine = $this->check($varName);
} else {
$foundLine = false;
}
if(!$foundLine){
$writeData = $varName.'__=__'.urlencode(serialize($varVal)).PHP_EOL;
file_put_contents($this->dataFile, $writeData, FILE_APPEND);
} else {
return "ERR_DX_KEY_ALREADY_EXISTS";
}
}
public function read($varName){
if(file_exists($this->dataFile)){
foreach(file($this->dataFile) as $line) {
if(strpos($line, $varName) !== false) {
list(, $new_str) = explode("__=__", $line);
$foundLine = true;
}
}
if($foundLine){
$val = rtrim($new_str);
return unserialize(urldecode($val));
} else {
return "ERR_DX_KEY_NOT_FOUND";
}
} else {
return "ERR_DX_FILE_DOES_NOT_EXIST";
}
}
public function del($varName){
if(file_exists($this->dataFile)){
$f = $this->dataFile;
$term = $varName.'__=__';
$arr = file($f);
foreach ($arr as $key=> $line) {
if(stristr($line,$term)!== false){unset($arr[$key]);break;}
}
//reindexing array
$arr = array_values($arr);
//writing to file
file_put_contents($f, implode($arr));
} else {
return "ERR_DX_FILE_DOES_NOT_EXIST";
}
}
public function modify($varName, $varVal){
if(file_exists($this->dataFile)){
if($this->check($varName)){
$this->del($varName);
}
}
$writeData = $varName.'__=__'.urlencode(serialize($varVal)).PHP_EOL;
file_put_contents($this->dataFile, $writeData, FILE_APPEND);
}
public function check($varName){
if(file_exists($this->dataFile)){
foreach(file($this->dataFile) as $line) {
if(stripos($line, $varName.'__=__') === 0){
return true;
}
}
return false;
} else {
return "ERR_DX_FILE_DOES_NOT_EXIST";
}
}
}
Usage:
<?php
require 'varDx.php';
$dx = new \varDx\cDX; //create object
$dx->def('file1.txt'); //define data file
$a = "this is a string";
$dx->write('val1', $a); //write key to file
$dx->modify('val1', "this is another string"); //modify value of key
echo $dx->read('val1'); //read value of key
if($dx->check('val1')){ //check if key exists
del('val1'); //delete key
}
File storage:
All keys are stored in this format:
keyname__=__urlencode(serialize(value_of_key))
There's more info on the functions in the README on the github page. I'm wondering if I can make this more efficient when dealing with files, and if there's anything else that I'm doing wrong?