转换并计算限制和使用情况,然后将其显示在消息中

时间:2017-12-23 09:58:10

标签: php codeigniter imap

您好我正在使用带有codeigniter的imap。我试图获取我的imap使用和限制消息就像谷歌有收件箱它有一个百分比显示像

0.07 GB (0%) of 15 GB used

目前我只能将我的一个显示为

(455112.26830953%) of 2.00 GB used

  

问题:如何确保我可以让imap计算限制和使用正确并将其显示在消息中。

当我回复所有电子邮件$count的总大小时,输出为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);
}

1 个答案:

答案 0 :(得分:1)

您的计算是落后的。您需要将$count除以$message['limit']而不是相反:

// 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

然后,您只需将$this->byte_convert($count)添加到输出中即可获得与Gmail相同的显示效果:

echo $this->byte_convert($count) . " (" . $percentage . "%) of " . $this->byte_convert($message['limit']) . " used";
// 460.80 KB (0.02%) of 2.00 GB used