I have small class called 'Call' and I need to store these calls into a flat file. I've made another class called 'CallStorage' which contains an array where I put these calls into.
My problem is that I would like to store this array to disk so I could later read it back and get the calls from that array.
I've tried to achieve this using serialize() and unserialize() but these seems to act somehow strange and part of the information gets lost.
This is what I'm doing:
//write array to disk
$filename = $path . 'calls-' . $today;
$serialized = serialize($this->array);
$fp = fopen($filename, 'a');
fwrite($fp, $serialized);
fclose($fp);
//read array from serialized file
$filename = $path . 'calls-' . $today;
if (file_exists($filename)) {
$handle = fopen($filename, 'r');
$contents = fread($handle, filesize($filename));
fclose($handle);
$unserialized = unserialize($contents);
$this->setArray($unserialized);
}
Can someone see what I'm doing wrong, or what. I've also tried to serialize and write arrays that contains plain strings. I didn't manage to get that working either.. I have a Java background so I just can't see why I couldn't just write an array to disk if it's serialized. :)
Firstly, use the shorthand forms:
file_put_contents($filepath,serialize($var));
and
$var=unserialize(file_get_contents($filepath));
And then output/debug at each stage to find where the problem is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With