我正在尝试使用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中结束,所以我尝试搜索很多有关此问题的信息,但没有结果。
我得到了阻止问题的根本原因,它是php会话,它阻止了来自同一用户的linux服务器中的所有其他请求(令人惊讶的是,在Windows上它运行良好)。
我用了session_write_close(); 在运行exec并解决问题之前,但是现在我在linux中有这个脚本的另一个问题,它发布在另一个问题中php exec / shell_exec / system / popen / proc_open在Linux上运行调用脚本本身的次数无数次
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句