PHP: How to safely include file with possible parse errors?
Another straight to the point solution
![](foto/Safe-PHP-include.jpg)
Unfortunately if there is an error in such file you will get parse error when including it.
Solution? Pretty simple- check the file before including it.
To check such file you can use php_check_syntax in PHP 5.0.4-. You can also use exec/system and run "php -l filename.php" command which works even in PHP newer than 5.04.
Here's a snippet you can use:
$res = exec("php -l datafile.php");
if (strstr($res, "No syntax errors detected"))
{
include_once 'datafile.php';
}
else //alternatively load a "safe" file:
{
include_once 'datafile.backup.php';
}
Comments