PHP flatfile storage So, I made a tiny (~2 KB, &lt;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:

&lt;?php
namespace varDX;

class cDX {
    private $dataFile;  

    public function def($filename){
        $this-&gt;dataFile = $filename;
    }

    public function write($varName, $varVal){
        if(file_exists($this-&gt;dataFile)){
            $foundLine = $this-&gt;check($varName);
        } else {
            $foundLine = false;
        }

        if(!$foundLine){
            $writeData = $varName.'__=__'.urlencode(serialize($varVal)).PHP_EOL;
            file_put_contents($this-&gt;dataFile, $writeData, FILE_APPEND);    
        } else {
            return "ERR_DX_KEY_ALREADY_EXISTS";
        }
    }

    public function read($varName){
        if(file_exists($this-&gt;dataFile)){
            foreach(file($this-&gt;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-&gt;dataFile)){
            $f = $this-&gt;dataFile;
            $term = $varName.'__=__';
            $arr = file($f);
            foreach ($arr as $key=&gt; $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-&gt;dataFile)){   
            if($this-&gt;check($varName)){
                $this-&gt;del($varName);
            } 
        }
        $writeData = $varName.'__=__'.urlencode(serialize($varVal)).PHP_EOL;
        file_put_contents($this-&gt;dataFile, $writeData, FILE_APPEND);    

    }

    public function check($varName){
        if(file_exists($this-&gt;dataFile)){
            foreach(file($this-&gt;dataFile) as $line) {
                if(stripos($line, $varName.'__=__') === 0){
                    return true;
                } 
            }
            return false;
        } else {
            return "ERR_DX_FILE_DOES_NOT_EXIST";
        }
    }
}


Usage:

&lt;?php
require 'varDx.php';
$dx = new \varDx\cDX; //create object
$dx-&gt;def('file1.txt'); //define data file

$a = "this is a string";
$dx-&gt;write('val1', $a); //write key to file

$dx-&gt;modify('val1', "this is another string"); //modify value of key
echo $dx-&gt;read('val1'); //read value of key

if($dx-&gt;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?
Assuming this code is solely for learning purpose, there are several main issues


to be honest, a function that returns ERR_DX_FILE_DOES_NOT_EXIST when I request an object looks a bit weird. I don't even know how to use it. Should I wrap every call to a function in a condition that checks the returned value? Or what else I am supposed to do with this string? Consider using errors/exceptions in case of error.
a race condition issue. you can have this file malformed if two parallel processes would try to write at the same time. Consider use file locks
an obvious memory issue - the bigger the file is, the more RAM does it take to process. PHP can let you to read a file line by line without memory overhead
such a clumsy format as keyname__=__urlencode(serialize(value_of_key)) doesn't seem to be very reliable. For such a toy storage I would rather use json format, to ease the search/key access.