I am currently using this command to validate some PHP files.
$op=null; $ret=null; exec("php -l '$file' 2>&1",$op,$ret);
Unfortunately on the customer's shared hosting (linux) it fails with the line below obviously because some commands are disabled:
Warning: exec(): Unable to fork [php -l '/path_to_the_file.php' 2>&1] in /my_program.php on line 559
I want to avoid this Warning at all costs because as soon as I disable debugging, the host shows its 500 error page which completely kills the webpage (for some strange reason).
Try/Catch does not work at all.
try {
$op=null; $ret=null; exec("php -l '$file' 2>&1",$op,$ret);
if($ret != 0) {
throw new Exception("'$file' failed syntax check");
}
} catch(Exception $e) {
$this->addLog(LOG_ERR, 'syntax error', $e);
continue;
}
Any ideas how to avoid this Warning?
This is the only solution that worked for me. I used a couple of functions to check if exec is available and enabled:
private function commandEnabled($comm) {
return is_callable($comm) && !($this->commandDisabled($comm));
}
private function commandDisabled($comm) {
return !(false === stripos(','.ini_get('disable_functions').',', ','.$comm.','));
}
if (!$this->commandEnabled('exec')) {
// I run my code here
}
UPDATE: I just found out from this post https://stackoverflow.com/a/29268475/1806085 that you can also catch the error in PHP 7+ with this:
try {
some_undefined_function_or_method();
} catch (\Error $ex) { // Error is the base class for all internal PHP error exceptions.
var_dump($ex);
}