note 80167 added to function.fopen
| From: | Konstantin-kks_maildoginboxdotru at osu1 dot php dot net | Date: | Wed, 02 Jan 2008 12:39:34 +0000 |
| Subject: | note 80167 added to function.fopen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-134658@lists.php.net to get a copy of this message | ||
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();
}
}
?>
----
Server IP: 195.54.192.44
Probable Submitter: 83.146.69.166
----
Manual Page -- http://www.php.net/manual/en/function.fopen.php
Edit -- https://master.php.net/note/edit/80167
Del: integrated -- https://master.php.net/note/delete/80167/integrated
Del: useless -- https://master.php.net/note/delete/80167/useless
Del: bad code -- https://master.php.net/note/delete/80167/bad+code
Del: spam -- https://master.php.net/note/delete/80167/spam
Del: non-english -- https://master.php.net/note/delete/80167/non-english
Del: in docs -- https://master.php.net/note/delete/80167/in+docs
Del: other reasons-- https://master.php.net/note/delete/80167
Reject -- https://master.php.net/note/reject/80167
Search -- https://master.php.net/manage/user-notes.php