Getting user quota stats I have the following function to collect quota information for the given user and return the information on a "ready to use" array:

/* Get User Quota Information
 *
 * Prepare and array with the user's account disk space information
 * by ascertaining the amount of space available on its quota,
 * the used space from that quota and free space remaining.
 *
 * @param string $user The system user name
 *
 * @return array
 */
function getUserQuotaInfo($user) {

  $quota = exec("quota -s -u ".$user);            // query server

  $quota = preg_replace("/[\s-]+/", ' ', $quota); // clear spaces

  $arr = explode(' ', $quota);

  $freeSpace = str_replace('M', '', $arr[3]) - str_replace('M', '', $arr[2]);

  return array(
    "total" =&gt; $arr[3],
    "used"  =&gt; $arr[2],
    "free"  =&gt; $freeSpace.'M'
  );
}


The function is working well and giving the expected results, but there's a lot going on to manipulate the information in order to have it ready to present to the user.

Is there any optimization that can be done as to reduce the possibilities of failure on this function as well as simplifying it?
I think you should use escapeshellarg to secure that $user does not do damage through your exec call. Also, you should check if your $arr actually has the required data.

As for optimization, replacing multiple spaces and dashes with single spaces and then exploding such strings seems to me slower than just splitting the string right away, using preg_split:

$arr = preg_split("/[\s-]+/", $quota);


However, you should still check that count($arr) &gt; 3.

I must say that I am a bit surprised to see this work for you. That means that the user under which PHP is running has the permission to see other users' quota. IMO, web server should not be allowed as much.

A better way to do this is to dump all users' quotas in some text file (i.e., using repquota command; see an example on how to use it here), via a cron job run as root, and then make the script that only parses that file.

Or, quite similarly, your cron job can make a file with all the data, i.e., create quota-data.php like this:

&lt;?php

$quota_data = array(
  'zuul' =&gt; array(
    'total' =&gt; ...,
    'used'  =&gt; ...,
    'free'  =&gt; ...,
  ),
  ...
);

?&gt;


which you then just include in your script and use the data.

Which of these two approaches is better depends on how often the data is read from the web. If rarely, the text file approach is better, so that parsing is done only when really needed; if often, the second approach is better, as it parses the data only once per update.