简体   繁体   中英

PHP Best practice for saving/storing very small amounts of data

What I'm doing currently works but I just have the feeling that there's a better way, or perhaps a more professional way to do it. I'm talking about very small amounts of data, like one number (or a few numbers) that is saved so as to store the selection made in a drop down list so its remembered every time the page is re-opened. I'm not using a mysql database at all so its all saved to file only.

Right now I simply save the number to a text file to save the selected folder, and then have several other numbers underneath it to save the selected files. All selected from drop down lists like this:

<form method="POST" action="index.php">
<select onchange="this.form.submit();" name="text">
<option>Select</option>
<option value="first">first</option>
<option value="second">second</option>
<option value="etc">etc</option>
</select>
</form>

Then they are stored in a text file that simply looks like this:

7
4
3

When the folder or file is altered the change is made with the following code:

function saveFile($line,$data,$savepath) {
$trace=file($savepath);$trace[$line]=$data;
file_put_contents($savepath,$trace);
}

if(isset($_POST['text'])) {
$changed=$_POST['text'];
saveFile($selected,$changed."\n",$savepath);
}

The code chooses which line of the text file to save to. Maybe there is a much better way or something completely different? Although I don't want to use sql or anything like that.

At the risk of downvotes for ignoring the following point:

Although I don't want to use sql or anything like that

You don't need to run a huge database system for this. Something along the lines of SQLite lets you have the functionality of the database, without huge overheads. The SQLite database is essentially a specific type of file with all of the relationships you configure, but gives a bit more flexibility than a flat file structure you're working with.

To me it looks you are attempting to persist few values from page load to page load. If this is only needed in the UI and not on the server-side I would save the data in a cookie ,but this is a rather dated solution to the problem ,also using local storage should do the trick for you. Sorry for the JavaScript solution and not PHP. Sample code:

localStorage.setItem("attr", "value");

alert( "attr: " + localStorage.getItem("attr"));

Another idea is to save your data parsed as Json, serialized (associative) array or similar to a file. That way you have named values and you know what value belongs to what function in your system if you open that file again in two years.

But again, if that hugely increases the complexity of your project or it begins to stop being efficient (because of the great amount of stored values), an optimised database, like others already recommended, would be the only way.

To save small amounts of data like this to file, I would recommend storing the data as key/value pairs in an array and then either serialize the array (or convert it to JSON) and then save that.

Eg

$folderValue = 5;
$file1Value = 6;
$file2Value = 7;

/**
  * Saving data
  */
$data = array(
    'folder' => $folderValue,
    'file1' => $file1Value,
    'file2' => $file2Value,
);

file_put_contents($filename , serialize(data));


/**
  * Getting data (this will be the same array you
  * saved to file at an earlier point) which you can
  * iterate over or grab values via key.
  */

$data = unserialize(file_get_contents($filename));

foreach ($data as $key => $value) {
    echo "key {$key} is {$value}\n";
    // e.g. "key folderValue is 5";
}

echo $data['folder'] . "\n";
// e.g. "5";

SQL is still a better candidate for storing application state. Flat files are poor because they become difficult to manage over time, but if you do use files, a key=>value mapping is important for readability.

SQLite could be a good candidate if you need a low overhead SQL (low configuration, no background server running), but it has its own limitations (low performance, doesn't scale).

PHP has good documentation around serializing data: http://php.net/manual/en/function.serialize.php http://php.net/manual/en/function.unserialize.php

Json is another good format for data serialization. Also see json_encode and json_decode.

看看火箭商店...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM