Serialize and Unserializa weird behaviour
From: Alex Malgaroli (lmollea_at_yahoo.it)
Date: 01/09/05
- Next message: Kurt Krueckeberg: "Re: complete newbie"
- Previous message: bfons: "Deny requests without a reverse lookup and/or Open Proxy"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Kurt Krueckeberg: "Re: complete newbie"
- Previous message: bfons: "Deny requests without a reverse lookup and/or Open Proxy"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|