3

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 whether 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?

4
  • 5
    I've read your question three times, and I still can't figure out what you're trying to do. Seems like you gave us your proposed solution to some unknown problem and then asked us if it is OK. Instead, tell us the problem you're trying to solve (and possibly include your proposed solution) and we'll tell you if it's the best way to solve it. Aug 2, 2015 at 18:13
  • @RobertHarvey I need to know wether what I'm doing is fine or wether I should move to mSQL. If that makes sense. Aug 2, 2015 at 18:16
  • 4
    For storing anything larger than configuration files, a SQL database or other well-known, persistent data store is almost always the best solution, especially on a public-facing web site. Aug 2, 2015 at 18:21
  • 2
    See also a similar question and the corresponding retrospective. Aug 3, 2015 at 12:04

3 Answers 3

4

If you want to go with the file approach and have mostly static and configuration data use SQLite (PHP entry): it is a RDBMS just like MySQL, but it is a local single file in a directory of your choice (it can even be in your application's folder), installation free, lightweight and can accomplish your data requirements with consistency and a lot of tested hard work behind it – there, all done for you without you having to worry about a new kind of data management. Data migration with SQLite involves basically moving/copying the .sqlite file.

For anything a little larger just go with MySQL or PostgreSQL.

1

Did you consider using some JSON-based noSQL database like MongoDB? It might perhaps fit your needs.

And you obviously can put JSON text in some SQL relational database like PostgreSQL, MariaDB (or MySQL) or Sqlite

Saving your persistent state as some PHP file is brittle, error-prone (beware of code injection), and probably not very efficient. Several languages better than PHP (e.g. HOP, Opa, Ocaml with Ocsigen, ...) are handling persistence (and mixing of browser & server computation) much better than PHP.

1
  • Thanks for the answer, so it is bad to be saving the current data to a .php file on the disk? Aug 3, 2015 at 11:28
-1

If you ask for my opinion and my advice. Go with MySQL because in the previous years I have built application using a JSON data structure but it has been very hard to maintain (it might also be my lack of knowledge).

JSON is cool and simple. I admit !! But not as powerful as MySQL.

Go with MySQL.

MySQL Vs JSON

1
  • 3
    don't just give your opinion, it's useless without a reason why.
    – jwenting
    Aug 3, 2015 at 11:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.