![](/img/trans.png)
[英]PHP How to Catch DeadlineExceededError on SendMail to continue Script execution
[英]PHP: How to kinda CATCH exec() WARNINGs & continue execution of main program
我目前正在使用此命令來驗證一些 PHP 文件。
$op=null; $ret=null; exec("php -l '$file' 2>&1",$op,$ret);
不幸的是,在客戶的共享主機 (linux) 上,由於某些命令被禁用,它明顯失敗並顯示以下行:
Warning: exec(): Unable to fork [php -l '/path_to_the_file.php' 2>&1] in /my_program.php on line 559
我想不惜一切代價避免此警告,因為一旦我禁用調試,主機就會顯示其 500 錯誤頁面,這會完全殺死該網頁(出於某種奇怪的原因)。
Try/Catch 根本不起作用。
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;
}
任何想法如何避免此警告?
這是唯一對我有用的解決方案。 我使用了幾個函數來檢查 exec 是否可用和啟用:
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
}
更新:我剛剛從這篇文章https://stackoverflow.com/a/29268475/1806085中發現,您還可以通過以下方式捕獲 PHP 7+ 中的錯誤:
try {
some_undefined_function_or_method();
} catch (\Error $ex) { // Error is the base class for all internal PHP error exceptions.
var_dump($ex);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.