【发布时间】:2014-03-05 18:15:56
【问题描述】:
我正在尝试使用 shell 命令检查 php 文件的语法错误,它在 Windows (WAMP) 中工作正常,但在 linux 上,由 shell 命令 exec/shell_exec/popen 等创建的进程永远不会终止,从而导致 apache 在这个过程被我强行杀死。杀死进程后也没有输出。
我的测试脚本是
文件 test.php 是一个示例 php 文件,仅用于测试,其中包含
<?php
$test['arr'] = 'bla';
?>
我尝试检查语法错误的代码是:
$slash = file_get_contents('test.php');
$tmpfname = tempnam("tmp", "PHPFile");
file_put_contents($tmpfname, $slash);
exec("php -l ".$tmpfname." 2>&1",$error); //also tried shell_exec but same behaviour
$errtext = '';
foreach($error as $errline) $errtext.='<br>'.$errline;
unlink($tmpfname);
echo $errtext;
也尝试使用函数 popen
$slash = file_get_contents('test.php');
$tmpfname = tempnam("tmp", "PHPFile");
file_put_contents($tmpfname, $slash);
$handle = popen("php -l ".$tmpfname." 2>&1", 'r');
$errtext = fread($handle, 2096);
pclose($handle);
unlink($tmpfname);
echo $errtext;
请有人指出我做错了什么以及为什么由 shell 命令创建的进程在 linux 中永远不会结束,我尝试了很多关于这个问题的搜索,但我没有得到任何结果。
【问题讨论】: