Our apache error_log was recently filled up with lines similar to the following (about 50GB):
[Wed Feb 01 16:50:15 2012] [error] [client 123.123.123.123] PHP Warning:
unpack() [<a href='function.unpack'>function.unpack</a>]:
Type V: not enough input, need 4, have 1
in /var/www/vhosts/domain.com/httpdocs/imagecreatefrombmp.php on line 52
Line 52 in imagecreatefrombmp.bmp is as follows:
$COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
This line is buried in a while loop.
If this issue happens again I want the code to quietly exit the while loop.
The problem is I cannot replicate the problem myself so I sort of need to solve it blind.
I’ve devised the following little solution. Would it serve the purpose? If the “Type V not input…” error occurs again would the try catch block catch it and return false?
try{
$COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
}catch (Exception $e) {
return FALSE;
}
You can’t catch a PHP error or warning as it is not an exception.
You can test, after calling
unpack, if an error was raised with error_get_last(), but that’s not really practical.Another solution is to set an error handler to catch the warning, and then throw an
ErrorExceptionfor that warning. You will then be able to use try/catch andreturn false;.Attention: all you errors, warnings, notices, etc… will be converted to an exception. That can potentially crash your program if you had one of those before.
Now you can catch the exception: