PDA

View Full Version : Need some help with Linux /proc and fread()



BrandonTheG
November 23rd, 2007, 00:55
Hello,

In my previous script, krakjoe said that I should use all PHP functions do that that stuff.

My delema is, the file /proc/cpuinfo shows up as 0 bytes on all the Linux servers I have access too, so using the following line won't work.


self::$cpuinfo = fread($loadcpuinfoFile, filesize(self::$cpuinfo_file));Now, since I am more concerned about learning how to write more efficient PHP classes, what would be the best way to overcome this?

Would setting this to any day old'n number work?

krakjoe
November 23rd, 2007, 05:21
I dunno why they show as zero filesize, however, all of these files are not going to exceed php's memory buffers, you should use file_get_contents because it's less instructions to interpret and so faster ( always remember php is interpreted so the less lines the better in general ), more efficient code ...

BrandonTheG
November 23rd, 2007, 16:24
Thanks, this is what I got as of now, might finish it up later, working on this server that keeps getting to the loads of the 1,000.


<?php

/**
* @author Brandon
* @copyright 2007 phpRiots
* @License http://creativecommons.org/licenses/by-sa/3.0/us/
* @Name cpuinfo
* @Description
*/

class cpuinfo {

private static $cpuinfo_file; // 'cpuinfo' file, leave this blank if you arn't sure or to use the default
private static $cpuinfo; // Will hold the information for the $cpuinfo_file string
private static $num_of_cpus; // Will hold the number of CPU's the system has, will be defined later
private static $cpuinfo_lines; // Will hold an array of each line of $cpuinfo


public function __construct () {

if (self::$cpuinfo_file == false) {

self::$cpuinfo_file = '/proc/cpuinfo';

}

self::$cpuinfo = file_get_contents(self::$cpuinfo_file);

self::$cpuinfo_lines = @explode("\n", self::$cpuinfo);

self::$num_of_cpus = substr_count(self::$cpuinfo, "processor");

}

private static function GenerateArray () {

for ($i = 0; $i <= self::$num_of_cpus; $i++){



}

}
}

?>