Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a vmail directory manage by postfix with the permissions set like this:

  • user: vmail
  • group: vmail
  • permission 700

I need to get the size of that directory with PHP. To do this, I'm using that code:

$io = popen('/usr/bin/du -sk '.$directory, 'r');

It work pretty well for the directories manage by PHP but not with the vmail directory because of the permissions.

The best solution that come to my mind is changing the user of the vmail directory to www-data.

What I'd like to know is what you guys think of this solution and if you think to a better solution to do it without reducing the security ?

share|improve this question
up vote 3 down vote accepted

I would not recommend to call a command like du directly from PHP. The problem is that its execution might take many minutes, and before it finishes your php's execution_timeout or some timeouts of the webserver might be reached.

I would rather recommend to do this in some kind of asynchronous way. Like for example having a nightly cronjob, which could run as any privileged user, executing the du and writing the result into a file. Then the PHP would only read this file when it needs to know the size.

Of course, that way the number can be up to 24 hours out of date. But at least you can be sure that your PHP will execute fast, and doesn't make any of the workers wait for a slow du.

share|improve this answer
Love that solution! Thanks a lot. Implementing it right away. – Nicolas BADIA Feb 16 '13 at 10:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .