从 Linux 操作系统使用 PHP 返回用户配额
我需要找出当前用户的配额详细信息,
我已经尝试过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.
要给出代码示例并扩展上述答案,
您需要 root 访问权限+网络服务器可读的目录。
执行
crontab -e
并添加
上面的行将每小时将用户
someusername
的配额保存在webserver文件夹中的quota.txt中。然后编写一个 PHP 文件来显示它
并不完美,如果您处于人类可读格式不是 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
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
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 PHPAlso 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