0

How to get the mail directory size from the file /var/mail/.../maildirsize in PHP?

The format of maildirsize looks like this:

20971520S,0C // quota storage, quota messages
1195 1 // size in bytes, message added
1687 1 // size in bytes, message added
-1195 -1 // size in bytes, message removed

Now I wrote my own function to get the maildirsize in total bytes and total messages. But it shows only correct values when I delete maildirsize on the server, re-login in my mail account so that the file maildirsize is newly generated. Any ideas why?

function calculateMaildirsize()
{
$total_bytes = 0;
$total_messages = 0;

$lines = file ('maildirsize');

foreach ($lines as $index => $line) {
    // Ignore Storage Quota and Message Quota $line[0]
    if($index > 0) {
        $line_value_bytes = explode(' ', $line)[0];
        $line_value_messages = explode(' ', $line)[1];

        $total_bytes += $line_value_bytes;
        $total_messages += $line_value_messages;
    }
}

$maildirsize = array('total_bytes' => $total_bytes,
                     'total_messages' => $total_messages);

return $maildirsize;
}

// Evoke the calculation function
$result = calculateMaildirsize();

// Output result
foreach($result as $key => $value) {
    echo $key." => ".$value."<br>\n";
}
udgru
  • 1,277
  • 5
  • 14
  • 26

1 Answers1

0

Perhaps this will work for you:

<?php
    $f = 'path/to/mail';
    $io = popen ( '/usr/bin/du -sk ' . $f, 'r' );
    $size = fgets ( $io, 4096);
    $size = substr ( $size, 0, strpos ( $size, "\t" ) );
    pclose ( $io );
    echo 'Directory: ' . $f . ' => Size: ' . $size;
?>

I have found it here some time ago and used it for a similar purpose - php get directory size

I hope that this helps!

Community
  • 1
  • 1
Michael Doye
  • 8,063
  • 5
  • 40
  • 56