Serialize and Unserializa weird behaviour

From: Alex Malgaroli (lmollea_at_yahoo.it)
Date: 01/09/05


Date: 9 Jan 2005 06:08:32 -0800

Hello everyone.

I found a strange behaviour using serialize and unserialize to persist
to file a configuration array.

I'm using a flat text file to store configuration information. I have
a function that lets software change the value of a configuration
variable; the function works like this: reads the entire file in a
string, unserialize it into an array, modify the correct variable then
rewrites the entire file serializing back the array.

code is like this:
        function preference_update($name, $val) {
                $fp = fopen('data/preferences.dat', 'r+');
                $ser = fread($fp, filesize('data/preferences.dat'));
                $prefs = unserialize($ser);

                for($i = 0; $i < count($prefs); $i++) {
                        if($prefs[$i]['name'] == $name) {
                                $prefs[$i]['value'] = $val;
                                break;
                        }
                }

                $ser = serialize($prefs);
                fseek($fp, 0);
                fwrite($fp, $ser);
                $this->mylog->debug($ser);
                fflush($fp);
                fclose($fp);
                unset($fp);
                unset($prefs);
                unset($ser);
        }
-----------

fun things I found are:
1) if I dont unset($fp) at the end, when doing multiple
"preference_update" in a single page, the first is written, the others
result in an empty file containing only "b:0;"
2) if I remove fwrite($fp, $ser) at the end, all things are correctly
written back! If I leave this line in the code, when calling multiple
times in the same script "preference_update", it seems that only the
first change gets serialized back.

(I know this is not efficient, but I don't have a database available.
No, either gdbm).

I'm using PHP 4.3.2 under apache 1.3.29 on cygwin both compiled from
source.

Any hint on what I'm doing wrong?

Thanks in advance.

LM



Relevant Pages

  • Re: Database suggestion
    ... I unserialize an 8MB file, which contains the db array, and put the ... have been using serialize() to store the arrays, ...
    (comp.lang.php)
  • Re: Serialize / Unserialize
    ... > Some have suggested that using serialize() ... > associated with calling unserialize(). ... Take the following array: ... That stands for a string with a length of 14. ...
    (comp.lang.php)
  • unserialize
    ... I finally got serialize to work but now I cannot get unserialize to work. ... $settings shows the unserialized data. ... but nothing is showing up for the array $settings_array when I do a print_r. ...
    (comp.lang.php)
  • Re: Keeping an Array in Memory
    ... I'm loading the array from a flat file with: ... > Does serialize() and unserialize ... Amir is right about using serialize/and unserialize. ... fileor keeping the array in a php file. ...
    (comp.lang.php)
  • Re: Serializing data set with de-normalized data
    ... You have very limited ability to affect the XML output and the only things ... array, you'll need to create each of those values and separate them with ... some delimmiter and ultimately treat them as one value. ... ComplexType and define your own effective Array, that should serialize ...
    (microsoft.public.dotnet.framework.adonet)