Solved

Exception Handling in PHP

Posted on 2011-03-04
2
279 Views
Last Modified: 2012-06-27
I have some code I would like to run that handle exceptions properly and I was wondering what the correct way to do it would be. I need the script to loop through database values, remove non UTF-8 values, and if there is an error record it in an array for emailing to me with successes and failures.

Here is my code
// DO SOMETHING WITH THE FILE DATA
function getMyFile($home,$away)
{
	$in = file($away);
	$out = fopen($home, "w");
		
	//REPLACE NON UTF-8 CHARACTERS AND INSERT INTO HOME FILE
	foreach ($in as $line) 
	{
		$line = preg_replace('/&(.)(acute|cedil|circ|lig|grave|ring|tilde|uml);/', "$1", $line);
		fputs($out, $line);
	}
}

$errorarray = array();
$feedcount = 0;

// LOOP THROUGH DB TO GET THINGS TO DO
while (loop_through_db_and_get_$home_and_$away)
{
	try
	{
		getMyFile($fetch['home'],$fetch['away']);
		$feedcount++;
	}
	catch (Exception $e)
	{
		$errorarray[$fetch['home']] = $e;
	}
}

// DO SOMETHING WITH THE ERRORS

if(count($errorarray) > 0)
{
	$subject = 'Run But With Errors - ' . $feedcount . " files ran smoothly.\n\n".count($errorarray)." files failed.";
	$body = "Here are the failures.\n\n";
	foreach ($errorarray as $file=>$error)
	{
		$body = $body . "File: " . $file . " failed with the following error.\n\n" . $error . "\n\n\n";
	}
}
else
{
	$subject = 'Run Smoothly - ' . $feedcount . " files.";
	$body = "Have a nice day.";
}

sendMail($subject,$body);

Open in new window


What is happening is that if the $home file does not exist the code stops and echos the error, but does not record the error in the array so the email shows a success.

Please help.

Thanks

Mike
0
Comment
Question by:hungoveragain
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 35035485
http://php.net/manual/en/function.fopen.php
check the value of fopen, if the result is FALSE, react accordingly
function getMyFile($home,$away)
{
	$in = file($away);
	$out = fopen($home, "w");

        if ($out === FALSE)
        {
           // file open failed
        }
        else
        {		
	//REPLACE NON UTF-8 CHARACTERS AND INSERT INTO HOME FILE
	foreach ($in as $line) 
	{
		$line = preg_replace('/&(.)(acute|cedil|circ|lig|grave|ring|tilde|uml);/', "$1", $line);
		fputs($out, $line);
	}
        }
}

Open in new window

0
 
LVL 3

Expert Comment

by:pius_babbun
ID: 35035669
Check this if it is helpful to you
// LOOP THROUGH DB TO GET THINGS TO DO
while (loop_through_db_and_get_$home_and_$away)
{
	if(getMyFile($fetch['home'],$fetch['away']))
	{
		$feedcount++;
	}
	else
	{
		$errorarray[$fetch['home']] 
	}
}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now