I’ve been building an application and the whole thing is built around a simple data storage system.
- Create an array.
- Convert it into JSON.
- Add
<?php die()
at the beginning to prevent access. - Save to PHP file and save to disk.
Then.
- Open file.
- Remove
<?php die();
. - Decode it from JSON.
- Edit/Read array.
- Save it back to file if edited.
Is this a safe way to store data? When I made this I didn’t know mySQL, and I still don’t know it that well. However, I want to make the choice to wether to edit my platform while it is small and change it to mySQL or to keep using the current system.
I’ve never encountered issues with a file being edited at the same time, and it doesn’t seem to make the website slower at the moment.
Is it worth changing my medium sized application now, and not face having to spend weeks changing it now it is growing? Or is the current system secure enough to keep using.
This is what the system currently works from;
Setting data:
function setDat($dat_file, $sec, $subsec, $val) {
global $INS_DIR;
$dat_file_dat = json_decode(str_replace("<?php die(); ", "", file_get_contents($_SERVER['DOCUMENT_ROOT'] . $INS_DIR . 'data/dat-'.$dat_file.'.php')), true);
$dat_file_dat[$sec][$subsec] = [$val];
file_put_contents($_SERVER['DOCUMENT_ROOT'] . $INS_DIR . 'data/dat-'.$dat_file.'.php', "<?php die(); ".json_encode($dat_file_dat));
}
Getting data:
function getDat($dat_file, $sec, $subsec) {
global $INS_DIR;
$dat_file_dat = json_decode(str_replace("<?php die(); ", "", file_get_contents($_SERVER['DOCUMENT_ROOT'] . $INS_DIR . 'data/dat-'.$dat_file.'.php')), true);
return $dat_file_dat[$sec][$subsec][0];
}
And so on.
If I explain this;
$dat_file
is the file that the data will be stored in, a file within /data/
$sec is the top level of the array you want to edit $array[$sec];
$subsec is the second level array $array[$sec][$subsec];
I do this because each application can use the $sec as the category for the data, and the $subsec and the search within their category. They can also have their own file. So the file main-settings has a category called theme-settings and then some information like active-theme which is set to default.
So this being said, it it worth keeping the current system and will it cope in the future? What are its weaknesses, and any problems with it.
OR
Should I start to change everything to mySQL, even though I will have to reprogram the whole data side of my program? What are the upsides to this and downsides?