I'm getting that error while trying to loop through my file using this code. If I
comment out my callback, it will read the first level(s) of my multidimensional
array, I'm also posting the function I use to write the file incase it might help.
Function with callback error (nesting limit 100 reached)
function readPacket($filename) {
if (!is_resource($filename)) { //are we a resource
if (!$file = fopen($filename,'r')) { return false; }
} else {
$file = $filename;
}
//setup chr(#), return array, k and v
$ret = array(); $key = ''; $val = null; $mod = 0;
while (!feof($file)) {
$b = fread($file, 1);
if (ord($b) < 9) {
if ($val != null) {
if ($mod == 2) { $val = (int)$val; }
if ($mod == 3) { $val = (string)$val; }
$ret[$key] = $val; $key = ''; $val = null; $mod = 0;
} else {
if (ord($b) == 0) { $mod=0; }
elseif (ord($b) == 1) { return $ret; }
else {
if ($mod == 5) { $key = (string)$key; }
if ($mod == 6) { $key = (int)$key; }
$mod = ord($b);
}
}
} else {
if ($mod == 5 || $mod == 6) { $key .= $b; }
elseif ($mod == 0) { $val = readPacket($file); }
else { $val .= $b; }
}
}
if (!is_resource($filename)) fclose($file);
return $ret;
}
$data = readPacket('this.txt');
var_export($data);
Function I use to actually write this array out (could be use for testing purposes)
function writePacket($filename, $b) {
if (!is_resource($filename)) { //create us if we aren't a resource
if (!$file = fopen($filename, 'w+')) return false;
} else {
$file = $filename;
}
//crypt - one-way encryption
$cipher_key = crypt($b['cipher'], SALT);
//new instance of blowfish
$bf = new Crypt_Blowfish('ecb');
$bf->setKey( $cipher_key );
foreach ($b as $key => $val) {
if($key != 'cipher') { //do not include our cipher
//add crypt_blowfish symmetric encryption
//fwrite($file, (is_int($key) ? chr(6) . $bf->encrypt((string)$key) : chr(5) . $bf->encrypt($key)));
fwrite($file, (is_int($key) ? chr(6) . (string)$key : chr(5) . $key));
if (is_array($val)) {
fwrite($file, chr(0)); //array starts
writePacket($file, $val);
fwrite($file, chr(1)); //array ends
} elseif (is_int($val)) {
//add crypt_blowfish symmetric encryption
//fwrite($file, chr(2) . $bf->encrypt((string)$val) . "\r"); //optional linebreak
fwrite($file, chr(2) . (string)$val . "\r"); //optional linebreak
} elseif (is_string($val)) {
//add crypt_blowfish symmetric encryption
//fwrite($file, chr(3) . $bf->encrypt($val) . "\r"); //optional linebreak
fwrite($file, chr(3) . $val . "\r"); //optional linebreak
}
}
}
//close resources
if (!is_resource($filename)) {
fclose($file);
return true;
}
}
//include random array
include('write.php');
writePacket('this.txt', $write);