fileName = $fileName; } /** * Getting value from from by key * * @param string $key * @return mixed */ public function get($key) { if (file_exists($this->fileName)) { $handle = fopen($this->fileName,"rb"); $contents = ''; $res = null; while (!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle); if (!empty($contents)) { $res = unserialize($contents); if (is_array($res)) { if (array_key_exists($key,$res)) { return $res[$key]; } } } } return null; } /** * Setting value and storing it into file * * @param string $key * @param mixed $value * @return bool */ public function set($key,$value) { if (file_exists($this->fileName)) { $handle = fopen($this->fileName,"rb"); $contents = ''; $res = null; while (!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle); if (!empty($contents)) { $res = unserialize($contents); } if (is_array($res)) { $res[$key] = $value; } else { $res = array($key => $value); } $wres = serialize($res); if(is_writable($this->fileName)) { $handle = fopen($this->fileName,"wb"); fwrite($handle,$wres); fclose($handle); return true; } } return false; } } /** How to use it? Store value in fb.txt: $f = new FileStorage(dirname(__FILE__).'/fb.txt'); $f->set($var_name,$var_value); Read this value later: $f = new FileStorage(dirname(__FILE__).'/fb.txt'); $value = $f->get($var_name); */ ?>