我已经进行了几个小时的研究,并找到了一种解决方案,但是我希望能够避免使用exec命令。
当前我有以下代码:
exec ("cat /proc/cpuinfo", $details);
$string= $details[4];
我希望它能够获取第一个处理器的类型,就像那个代码一样,但是不使用system或exec命令。
我更喜欢直接从/proc/cpuinfo中读取。
我已经进行了几个小时的研究,并找到了一种解决方案,但是我希望能够避免使用exec命令。
当前我有以下代码:
exec ("cat /proc/cpuinfo", $details);
$string= $details[4];
我希望它能够获取第一个处理器的类型,就像那个代码一样,但是不使用system或exec命令。
我更喜欢直接从/proc/cpuinfo中读取。
file
:$file = file('/proc/cpuinfo');
$proc_details = $file[4];
model name : Intel(R) Core(TM)2 Quad CPU Q8300 @ 2.50GHz
。我该如何去掉“model name:”这部分? - Jacob$proc_details = substr($proc_details, 13);
- lonesomeday只需将以下代码放置:
<pre>
<strong>Uptime:</strong>
<?php system("uptime"); ?>
<br />
<strong>System Information:</strong>
<?php system("uname -a"); ?>
<br />
<strong>Memory Usage (MB):</strong>
<?php system("free -m"); ?>
<br />
<strong>Disk Usage:</strong>
<?php system("df -h"); ?>
<br />
<strong>CPU Information:</strong>
<?php system("cat /proc/cpuinfo | grep \"model name\\|processor\""); ?>
</pre>
建议: 使用file_get_contents()函数是很好的选择,但您需要检查php_open_basedir限制和文件是否存在。
$cpuget = file('/proc/cpuinfo');
$cpu["name"] = str_replace(" ", " ",substr(str_replace("model name : ", "", $cpuget[4]),0,-1));
$cpu["cores"] = substr(str_replace("cpu cores : ", "", $cpuget[12]),0,-1);
$cpu["cache"] = substr(str_replace("cache size : ", "", $cpuget[8]),0,-1);
file_get_contents("/proc/cpuinfo")
,但我不知道是否有效... 可能不会,但可以试一下。为什么不能使用exec
? - Pekkaexec()
必须谨慎使用,但_仅适用于用户生成的输入_。虽然 OP 可能会担心此事,但对于纯服务器端的东西,exec()
是可以接受的。 - Bojanglesexec
非常慢。一个简单的基准测试表明,差异大约是50倍... - lonesomeday