Created
June 19, 2018 17:40
-
-
Save Zorono/4394c748cf583c4991fade86d69f8245 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function OpenFile($fini, $mode = 'w', $data = array('tag' => '', 'fields' => array()), $ac_on_error = False) | |
{ | |
$File = array(); | |
switch($mode) | |
{ | |
case 'w': { | |
$File['file'] = fopen($fini, 'a') or new \Exception('[INI-Handler]: Couldn\'t open the speciefied "'. $fini .'" file!'); | |
if((is_array($data) && is_array($data['fields'])) && (count($data) >= 1 && count($data['fields']) >= 1)) | |
{ | |
fwrite($File['file'], '[' .$data['tag']. ']\r\n'); | |
foreach($data['fields'] as $key => $value) | |
{ | |
if($key == end($data['fields'])) | |
{ | |
fwrite($File['file'], $key .'='. $value. '\r'); | |
} | |
else | |
{ | |
fwrite($File['file'], $key .'='. $value. '\r\n'); | |
} | |
} | |
$File['fields'] = $data['fields']; | |
} | |
else | |
{ | |
throw new \Exception('[INI-Handler]: Failed to Write to the file: ' .$fini. ' (There must be a valid data[\'tag\', \'fields\'] argument)'); | |
} | |
} | |
case 'r': { | |
if($File['file'] = file_get_contents($fini)) | |
{ | |
$_SOURCE = explode('\n', $File['file']); | |
for($f = 0; $f < count($_SOURCE); $f++) | |
{ | |
$fields = explode('=', $_SOURCE[$f]); | |
foreach($fields as $key => $value) | |
{ | |
$File['fields'][$key] = $value; | |
} | |
} | |
} | |
else | |
{ | |
if($ac_on_error) | |
{ | |
$File['file'] = fopen($fini, 'a') or new \Exception('[INI-Handler]: Couldn\'t open the speciefied "'. $fini .'" file!'); | |
} | |
else | |
{ | |
throw new \Exception('[INI-Handler]: Couldn\'t find speciefied INI File: ' .$File['file']); | |
} | |
} | |
} | |
} | |
return $File['fields']; | |
} | |
/* | |
USAGE: | |
$file = OpenFile(dirname(__FILE__) . '/notes.ini', 'w', array('tag' => 'Hey all', | |
'fields' => array('note1' => 'value1', | |
'note2' => 'value2', | |
'note3' => 'value3'))); | |
var_dump($file); | |
$file = OpenFile(dirname(__FILE__) . '/notes.ini', 'r', NULL, False); | |
var_dump($file); | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment