PHP flatfile storage
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 varDxcDX; //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?
php database library
add a comment |
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 varDxcDX; //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?
php database library
add a comment |
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 varDxcDX; //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?
php database library
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 varDxcDX; //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?
php database library
php database library
asked 21 mins ago
rahuldottech
1144
1144
add a comment |
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210602%2fphp-flatfile-storage%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210602%2fphp-flatfile-storage%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown