вот такой класс нашёлся Код (PHP): if (!defined('_BR_')) define('_BR_',chr(13).chr(10)); class TIniFileEx { public $filename; public $arr; function __construct($file = false){ if ($file) $this->loadFromFile($file); } function initArray(){ $this->arr = parse_ini_file($this->filename, true); } function loadFromFile($file){ $result = true; $this->filename = $file; if (file_exists($file) && is_readable($file)){ $this->initArray(); } else $result = false; return $result; } function read($section, $key, $def = ''){ if (isset($this->arr[$section][$key])){ return $this->arr[$section][$key]; } else return $def; } function write($section, $key, $value){ if (is_bool($value)) $value = $value ? 1 : 0; $this->arr[$section][$key] = $value; } function eraseSection($section){ if (isset($this->arr[$section])) unset($this->arr[$section]); } function deleteKey($section, $key){ if (isset($this->arr[$section][$key])) unset($this->arr[$section][$key]); } function readSections(&$array){ $array = array_keys($this->arr); return $array; } function readKeys($section, &$array){ if (isset($this->arr[$section])){ $array = array_keys($this->arr[$section]); return $array; } return array(); } function updateFile(){ $result = ''; foreach ($this->arr as $sname=>$section){ $result .= '[' . $sname . ']' . _BR_; foreach ($section as $key=>$value){ $result .= $key .'='.$value . _BR_; } $result .= _BR_; } file_p_contents($this->filename, $result); return true; } function __destruct(){ $this->updateFile(); } } пробую добавить это в свой файл.php и ниже код: Код (PHP): <?php $ini = new TIniFileEx('configsssss.ini'); // создаем объект $ini->write('main','key1','100'); // пишем в секцию main $ini->write('main','key2','200'); $ini->updateFile(); // скидываем информацию в ini файл ?> но ничего не работает, и файл не создаётся :-( или класс не рабочий или я не правильно чего делаю? PHP, JavaScript, SQL и другой код пишите внутри тегов Код ( (Unknown Language)): [b]php][/b]Тут код[b][/[/b][b]code][/b][/color]
ну я же прям в этот же файл класс добавила... ладно попробую ща в отдельный файл и потом подключить его.
нифига не понимаю, толи я тупая такая, толи класс всё таки не рабочий... вот создала файл TIniFileEx.php Код (PHP): <?php /** * Created by PhpStorm. * User: alisa * Date: 29.10.15 * Time: 19:30 */ if (!defined('_BR_')) define('_BR_',chr(13).chr(10)); class TIniFileEx { public $filename; public $arr; function __construct($file = false){ if ($file) $this->loadFromFile($file); } function initArray(){ $this->arr = $this->parse_ini_custom($this->filename); } function loadFromFile($file){ $result = true; $this->filename = $file; if (file_exists($file) && is_readable($file)){ $this->initArray(); } else $result = false; return $result; } function read($section, $key, $def = ''){ if (isset($this->arr[$section][$key])){ return $this->arr[$section][$key]; } else return $def; } function write($section, $key, $value){ if (is_bool($value)) $value = $value ? 1 : 0; $this->arr[$section][$key] = $value; } function eraseSection($section){ if (isset($this->arr[$section])) unset($this->arr[$section]); } function deleteKey($section, $key){ if (isset($this->arr[$section][$key])) unset($this->arr[$section][$key]); } function readSections(&$array){ $array = array_keys($this->arr); return $array; } function readKeys($section, &$array){ if (isset($this->arr[$section])){ $array = array_keys($this->arr[$section]); return $array; } return array(); } function updateFile(){ $result = ''; foreach ($this->arr as $sname=>$section){ $result .= '[' . $sname . ']' . _BR_; foreach ($section as $key=>$value){ $result .= $key .'='.$value . _BR_; } $result .= _BR_; } file_p_contents($this->filename, $result); return true; } function parse_ini_custom ( $filepath ) { $ini = file( $filepath ); if ( count( $ini ) == 0 ) { return array(); } $sections = array(); $values = array(); $globals = array(); $i = 0; foreach( $ini as $line ){ $line = trim( $line ); // Comments if ( $line == '' || $line{0} == ';' ) { continue; } // Sections if ( $line{0} == '[' ) { $sections[] = substr( $line, 1, -1 ); $i++; continue; } // Key-value pair list( $key, $value ) = explode( '=', $line, 2 ); $key = trim( $key ); $value = trim( $value ); if ( $i == 0 ) { // Array values if ( substr( $line, -1, 2 ) == '[]' ) { $globals[ $key ][] = $value; } else { $globals[ $key ] = $value; } } else { // Array values if ( substr( $line, -1, 2 ) == '[]' ) { $values[ $i - 1 ][ $key ][] = $value; } else { $values[ $i - 1 ][ $key ] = $value; } } } for( $j=0; $j<$i; $j++ ) { $result[ $sections[ $j ] ] = $values[ $j ]; } return $result + $globals; } function __destruct(){ $this->updateFile(); } } ?> потом в index.php такое: Код (PHP): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> <title></title> </head> <body> <?php require 'TIniFileEx.php'; $ini = new TIniFileEx('./config.ini'); // создаем объект $ini->write('main','key1','100'); // пишем в секцию main $ini->write('main','key2','200'); $ini->updateFile(); ?> </body> </html> реакции ноль, даже если в ручную создать этот файл, ничего туда не пишется... А вот если попробовать прочитать с помощью этого класса то работает! Код (PHP): $key1 = $ini->read('main','key1','100'); вот это работает! но читать я и так умею через parse_ini_file, мне надо именно записать как то переменные в файл :-( PHP, JavaScript, SQL и другой код пишите внутри тегов Код ( (Unknown Language)): [b]php][/b]Тут код[b][/[/b][b]code][/b][/color]
всё! нашла file_p_contents($this->filename, $result); Это походу препод специально так издевается над нами