转换并计算限制和使用量,然后在消息中显示

Convert and calculate the limit and usage and then display it in a message

您好,我正在将 imap 与 codeigniter 一起使用。我正在尝试获取我的 imap 使用和限制消息,就像 google 在收件箱上显示的百分比一样

0.07 GB (0%) of 15 GB used

目前我只能让我的显示像

(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.

当我回显所有电子邮件的总大小时 $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);
}

你的计算落后了。您需要将 $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