从 Linux 操作系统使用 PHP 返回用户配额

发布于 2024-08-22 20:55:41 字数 119 浏览 3 评论 0原文

我需要找出当前用户的配额详细信息,

我已经尝试过 exec("配额'用户名'", $retParam) 还有 system() 但没有返回任何内容。

有什么想法吗?

谢谢。

I need to find out quota details for the current user,

I`ve tried
exec("quota 'username'", $retParam)
and also system() but nothing is returned.

Any ideas ?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

埋情葬爱 2024-08-29 20:55:41

PHP 运行的用户可能不允许获得其他用户的配额 - 甚至可能不允许其自己的配额,或者甚至不允许执行外部命令。根据您的服务器设置,您也许可以更改 PHP 的配置(例如删​​除 safe_mode)并提升 PHP 用户的权限,但我不知道这是否是明智的做法。如果您使用共享托管,则必须与您的提供商联系是否可以采取任何措施。

此博客条目概述了一种巧妙的方法来获取通过在外部 cron 作业中收集文本文件中的所有配额,并使用 PHP 解析该文件,绕过 PHP 的限制。当然,只有当您有权访问服务器,或者可以使用比 PHP 用户更自由的权限设置 cron 作业时,才有效。

The user PHP runs under is probably not allowed to get other users' quotas - maybe not even its own, or maybe it's not even allowed to execute external commands. Depending on your server setup, you may be able to change PHP's configuration (remove safe_mode for example) and elevate the rights of the PHP user, but I don't know whether that's a wise thing to do. If you are on shared hosting, you would have to speak to your provider whether anything can be done.

This blog entry outlines a clever way to get around PHP's limitations by collecting all quotas in a text file in an external cron job, and parsing that file with PHP. Works only if you have access to the server, of course, or can set up cron jobs with more liberal permissions than the PHP user.

陪你到最终 2024-08-29 20:55:41

要给出代码示例并扩展上述答案,

您需要 root 访问权限+网络服务器可读的目录。

执行crontab -e

并添加

0 * * * * quota -u -s someusername | grep /dev > /path/to/webserver/quota.txt

上面的行将每小时将用户someusername的配额保存在webserver文件夹中的quota.txt中。

然后编写一个 PHP 文件来显示它

$quota_txt= file_get_contents("quota.txt");
$quota_arr = explode("   ",$quota_txt);
function rm_g($str){
return intval(substr_replace($str, "", -1));

}
echo "total quota: ". $quota_arr[4]."<br>";
echo "used: ". $quota_arr[3]."<br>";
$free =(rm_g($quota_arr[4]))-rm_g($quota_arr[3]);
echo "free: " . $free . "G";


并不完美,如果您处于人类可读格式不是 G(千兆字节)的用例中,则需要对其进行调整,在这种情况下删除 -s 和解析字节,然后将字节输出转换为 PHP 中人类可读的格式。

另外,您可能已经猜到,cronjob 是每小时一次,这意味着用户无法获取实时统计数据,可能会出现一些不一致的情况。您可能想增加 cron 的频率。

另一种强化方法是如上所述获取配额,但每次都执行 du 以获得真正使用的空间。 (在这里查找“使用 PHP 有效获取文件夹大小”,您将找到代码)。这将为您提供实时剩余配额

To give a code example and extend the above answer

You need root access + a directory that is readable by your webserver.

do crontab -e

and add

0 * * * * quota -u -s someusername | grep /dev > /path/to/webserver/quota.txt

The above line will save the quota of user someusername in quota.txt located in the webserver folder every hour.

Then write a PHP file to show it

$quota_txt= file_get_contents("quota.txt");
$quota_arr = explode("   ",$quota_txt);
function rm_g($str){
return intval(substr_replace($str, "", -1));

}
echo "total quota: ". $quota_arr[4]."<br>";
echo "used: ". $quota_arr[3]."<br>";
$free =(rm_g($quota_arr[4]))-rm_g($quota_arr[3]);
echo "free: " . $free . "G";


Not perfect, and it need to be adapted if you are in a use-case where the human-readable format is not G (gigabytes), in that case remove the -s and parse bytes then convert the bytes output to human-readable in PHP

Also as you may have guessed, the cronjob is hourly, which means user is not getting realtime stats, there might be some inconsistencies. you may want to increase the frequency of the cron.

Another intensive approach is to get quota as above, but do du every time to get really used space. (look for "get folder size with PHP efficiently" here and you will find codes). This will get you the real-time remaining quota

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文