Hi I am using imap with codeigniter. I am trying to get my imap usage and limit message just like google has on there inbox it has a percentage displayed like
0.07 GB (0%) of 15 GB used
Currently I can only get my one to display like
(455112.26830953%) of 2.00 GB used
Question: How can I make sure I can get the imap to caculate the limit and usage correct and display it in a message.
When I echo the total size of all email $count
the out put is 471858
if (isset($results)) {
if ($order == 'DESC') {
$lists = array_reverse($results);
}
$count = 0;
foreach ($lists as $overview) {
$count += $overview->size;
if (isset($overview->subject)) {
echo $overview->msgno ." ". $overview->date ." ". $overview->from ." ". $overview->subject . "<br/>";
} else {
echo $overview->msgno ." ". $overview->date ." ". $overview->from ." ". "No Subject<br/>";
}
}
// returns the total email sizes
echo $count;
echo "<br/><br/>";
$quota = imap_get_quotaroot($mbox, "INBOX");
$message = $quota['MESSAGE'];
$percentage = ($message['limit'] / $count) * 100;
echo "(" . $percentage . "%) of " . $this->byte_convert($message['limit']) . " used";
}
function byte_convert($size) {
# size smaller then 1kb
if ($size < 1024) return $size . ' Byte';
# size smaller then 1mb
if ($size < 1048576) return sprintf("%4.2f KB", $size/1024);
# size smaller then 1gb
if ($size < 1073741824) return sprintf("%4.2f MB", $size/1048576);
# size smaller then 1tb
if ($size < 1099511627776) return sprintf("%4.2f GB", $size/1073741824);
# size larger then 1tb
else return sprintf("%4.2f TB", $size/1073741824);
}
Your calculation is backward. You need to divide $count
by $message['limit']
instead of the other way around:
// 2 GB = 2147483648 bytes
$percentage = ($message['limit'] / $count) * 100;
// (2147483648 / 471858) * 100 = 455112% = incorrect
$percentage = ($count / $message['limit']) * 100;
// (471858 / 2147483648) * 100 = 0.02% = correct
Then you can just add $this->byte_convert($count)
to your output to get the same display as in Gmail:
echo $this->byte_convert($count) . " (" . $percentage . "%) of " . $this->byte_convert($message['limit']) . " used";
// 460.80 KB (0.02%) of 2.00 GB used