note 80167 deleted from function.fopen by felipe

From: Date: Thu, 03 Jan 2008 01:31:30 +0000
Subject: note 80167 deleted from function.fopen by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-134676@lists.php.net to get a copy of this message
Note Submitter: Konstantin - kks_mail dog inbox dot ru ---- Hi, here is my class for manage config files. (php-config for example, or some others..) <?php class Configurer { var $file; var $lines; function __construct($config) { if (!file_exists($config)) return false; $this->lines = array(); $this->file = fopen($config, "r"); if ($this->file) { while (!feof($this->file)) { $str = trim(fgets($this->file, 4096)); $this->lines[] = $str; } fclose($this->file); $this->file = fopen($config, "w+"); } return true; } function set_var($var, $val, $quote = "\"") { if (!$this->file) return false; $i = 0; if ($this->lines) foreach ($this->lines as $line) { $l = explode("=", $line); if (trim($l[0]) == $var) // found var { $this->lines[$i] = $var." = ".$quote.$val.$quote; return true; } $i++; } // not found: $this->lines[] = $var." = ".$quote.$val.$quote; return true; } function save($php = true) { if (!$this->file) return false; $src = ""; if (!count($this-lines) && $php) { $src = "<?php\n\n".$src; $src .= "\n\n?>"; } if ($this->lines) foreach ($this->lines as $line) if (!$src) $src = $line; else $src .= "\n".$line; fwrite($this->file, $src); fclose($this->file); } function __destruct() { $this->save(); } } ?>

« previous php.notes (#134676) next »