Page 1 of 1

fopen - many times per page

Posted: Wed Aug 01, 2012 11:26 am
by IGGt
I've put together a debug function, that is set to run at various point in my site. But as I add more instances of this function I am starting to get more and more errors in the form of:

[text]Warning: fopen(.\DebugData\PageListing_20120801_17.txt): failed to open stream: No such file or directory in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Test\debug.function.php on line 96 Can't Open File[/text]

In some cases it is accessed up to 25 times per page, to record every page that is loaded, every Database that is accessed, and every query that is run etc.

Does it matter that I open and close the debug file each time I write to it, or is there a better option?

My function is:

Code: Select all

function debugf($P,$S,$D,$Ds)
{
	$debug = "On";
	
	if ($debug == "On") {
	
	$timezone = "Europe/London";
	date_default_timezone_set ($timezone);
	
	$DebugDate = date('Ymd_H');
	$ResetTime = date('Y-m-d H:i:s');			
	$tab = (chr(9));		

	$page = $P;
	$Section = explode(",",$S);
	$DB = $D;
	$DBPoint = $Ds;
	
	//print_r($Section);
	
	//$a=0;
	foreach($Section AS $Sc){
		switch($Sc){
			case 1:
				//echo "COUNTER";
				$myFile = ".\\DebugData\\PageListing_".$DebugDate.".txt";
				if(isset($_SESSION['debugCounter'])){$Counter = $_SESSION['debugCounter'];}else{$Counter = 1;};
				$fh = fopen($myFile, 'a') or die("Can't Open File");
				flock($fh, LOCK_EX);
				fwrite($fh, "--PAGE COUNT: ".$Counter."--\r\n");
				flock($fh, LOCK_UN);
				fclose($fh);
				print $Counter;
				$Counter++;
				$_SESSION['debugCounter'] = $Counter;
				break;
			case 2:
				//echo "DBCONNECTIONS";
				switch($DBPoint){
					case 1:
						$DBp = "Start";
						break;
					case 2:
						$DBp = "Connected";
						break;
					case 3:
						$DBp = "End";
						break;
					default:
						$DBp = $Ds;
						break;
					};
				$myFile = ".\\DebugData\\DBConnections_".$DebugDate.".txt";
				$fh = fopen($myFile, 'a') or die("Can't Open File");
				flock($fh, LOCK_EX);
				fwrite($fh, $page.$tab.date('H:i:s').$tab.$DB." ".$DBp."\r\n");
				flock($fh, LOCK_UN);
				fclose($fh);
				break;
			case 3:
				//echo "EMAIL ON RESET";
				if(isset($_SESSION['debugCounter'])){$Counter = $_SESSION['debugCounter'];}else{$Counter = 2;};
				if($Counter == 2) {
					$email = "email@company.com";
					$subj = "Test Page Counter Has Been Reset";
					$mes = "Test Page Counter Has Been Reset\n 
					Time: $ResetTime";
					mail($email, $subj, $mes);
				};
				unset($Counter);
				break;
			case 4:
				//echo "HEADER";
				//DBConnections
				$myFile = ".\\DebugData\\DBConnections_".$DebugDate.".txt";
				$fh = fopen($myFile, 'a') or die("Can't Open File");
				flock($fh, LOCK_EX);
				fwrite($fh, "## START ##".$tab."Time".$tab."Database \r\n");
				flock($fh, LOCK_UN);
				fclose($fh);
				//PageListing//
				$myFile = ".\\DebugData\\PageListing_".$DebugDate.".txt";
				$fh = fopen($myFile, 'a') or die("Can't Open File");
				fwrite($fh, "## ".$page." ##".$tab."Time".$tab."Current".$tab."Peak \r\n");
				fclose($fh);
				break;
			case 5:
				//echo "PAGE LISTING";
				$mem = memory_get_usage(true); //in bytes
				$memPeak = memory_get_peak_usage (true); //in bytes
				$myFile = ".\\DebugData\\PageListing_".$DebugDate.".txt";
				$fh = fopen($myFile, 'a') or die("Can't Open File");
				flock($fh, LOCK_EX);
				fwrite($fh, $page.$tab.date('H:i:s').$tab.$mem.$tab.$memPeak."\r\n");
				flock($fh, LOCK_UN);
				fclose($fh);
				break;
			};
		//$a++;	
	};
	};
};
and is called as:

Code: Select all

//DEBUG (Page Listing)
debugf("pageName.php(START)","5",0,0);	
//--

//DEBUG (DB Connection)
debugf("pagename.php","2",$Cserver,3);	
//--

Re: fopen - many times per page

Posted: Wed Aug 01, 2012 10:03 pm
by requinix
A database is a better option. With files you have to deal with concurrency and that is not fun.

Re: fopen - many times per page

Posted: Thu Aug 02, 2012 7:32 am
by IGGt
My only concern with that is that if I am opening and closing a DB connection 25+ times per page (apx 400 times for a complete load, including all includes etc.), isn't that going to add a lot of overhead, and time?

Re: fopen - many times per page

Posted: Thu Aug 02, 2012 4:48 pm
by requinix
Yes. Which is why you don't keep opening and closing it. Open once when you need it the first time, close it if you want to at the end (or let PHP do it for you).

Re: fopen - many times per page

Posted: Fri Aug 03, 2012 5:30 pm
by pickle
You should translate this to a class. In that class, you can open the file once (in the __construct() function) then write to it multiple times. The file will be closed automatically by PHP when your script completes.