Efficiently managing user disk space is vital for maintaining a healthy server environment. This article explores several PHP-based methods for retrieving user quota information on a Linux system, catering to various scenarios and preferences. We’ll cover techniques ranging from leveraging standard Linux commands to utilizing database solutions and IMAP functions, focusing on secure and robust implementation.
Table of Contents
- Using
repquota
for System-Wide Quota Reporting - Estimating Usage with
du
- Retrieving Quotas from a MySQL Database
- Accessing IMAP Mailbox Quotas
- Scheduling Quota Checks with Cron
Using repquota
for System-Wide Quota Reporting
The repquota -a
command provides a comprehensive report of user quotas across your file system. PHP can execute this command using shell_exec()
and parse the resulting output. Critical Security Note: Always sanitize user inputs before incorporating them into shell commands to prevent command injection vulnerabilities.
&1'); // 2>&1 redirects stderr to stdout for better error handling
if ($output === null || strpos($output, 'repquota: ') === 0) { //Check for errors in repquota execution
error_log("Error executing repquota: " . $output);
echo "Error retrieving quota information.";
exit(1);
}
$lines = explode("n", trim($output));
$quotas = [];
foreach ($lines as $line) {
if (strpos($line, "/") !== false) { //Skip header lines
continue;
}
$parts = preg_split('/s+/', $line); //Use preg_split for robust space handling
if(count($parts) > 5){ //Ensure sufficient data exists
$username = $parts[0];
$used = $parts[1];
$limit = $parts[2];
$quotas[$username] = ['used' => $used, 'limit' => $limit];
}
}
print_r($quotas); //Display results
?>
Estimating Usage with du
The du -sh
command provides a summary of disk usage for a given directory. While not a direct quota retrieval, it’s useful for estimating the disk space consumed by a user’s home directory. Remember to handle potential errors, such as the directory not existing.
&1");
if ($output === null || strpos($output, "du: cannot access") === 0) {
return false; //Handle errors gracefully
}
list($size, $path) = explode("t", trim($output));
return $size;
}
$username = "user1";
$homeDir = "/home/{$username}";
$size = getDirectorySize($homeDir);
if ($size !== false) {
echo "{$username}'s home directory size: {$size}n";
} else {
echo "Error getting directory size for {$username}n";
}
?>
Retrieving Quotas from a MySQL Database
If you maintain a MySQL database to track user quotas, PHP’s MySQLi extension offers a convenient way to retrieve this information. Remember to use parameterized queries to prevent SQL injection.
connect_errno) {
die("Connection failed: " . $mysqli->connect_error);
}
$stmt = $mysqli->prepare("SELECT quota FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$username = "user1";
$stmt->execute();
$stmt->bind_result($quota);
$stmt->fetch();
$stmt->close();
$mysqli->close();
echo "{$username}'s quota: " . $quota . "n";
?>
Accessing IMAP Mailbox Quotas
The imap_get_quota()
function is specifically designed to retrieve mailbox quotas for IMAP servers. This is relevant if you manage email accounts and their storage limits.
usage . " bytes of " . $quota->limit . " bytesn";
imap_close($imapStream);
} else {
echo "Failed to connect to IMAP server: " . imap_last_error() . "n";
}
?>
Scheduling Quota Checks with Cron
For automated quota monitoring, leverage cron jobs. A shell script can run repquota
, and a PHP script can process the output, generating reports or alerts. This allows for regular checks without constant PHP process execution.