aga*_*ggi 4 php linux shell exec
我有一个脚本,使用php -l检查php文件中的语法错误.它在Windows中工作正常但在Linux中输出不正确:
正在检查语法错误的文件exec_ip.php的内容是(它有要检查的语法错误):
<?php
$arr['12] = 'asd';
?>
Run Code Online (Sandbox Code Playgroud)
并且脚本是:
$slash = file_get_contents('exec_ip.php');
//echo $slash;
$tmpfname = tempnam("tmp", "PHPFile");
file_put_contents($tmpfname, $slash);
exec("php -l ".$tmpfname,$error);
$errtext = '';
foreach($error as $errline) $errtext.='<br>'.$errline;
unlink($tmpfname);
echo 'ERR:'.$errtext;
Run Code Online (Sandbox Code Playgroud)
结果在WINDOWS(WAMP){CORRRECT}:
ERR:
Parse error: syntax error, unexpected T_STRING, expecting ']' in C:\WINDOWS\Temp\PHP1F1.tmp on line 2
Errors parsing C:\WINDOWS\Temp\PHP1F1.tmp
Run Code Online (Sandbox Code Playgroud)
LINUX中的结果(Centos/cPanel){UNKNOWN OUTPUT}:
ERR:
Content-type: text/html
ERR:
Content-type: text/html
ERR:
Content-type: text/html
ERR:
Content-type: text/html
ERR:
Content-type: text/html
ERR:
Content-type: text/html
ERR:
Content-type: text/html
ERR:
Content-type: text/html
ERR:
... too many same above lines
Run Code Online (Sandbox Code Playgroud)
请有人帮助我,并指出为什么它在linux生产服务器中提供不正确的输出.我也尝试使用shell_exec,popen,proc_open,system代替exec,但都有相同的行为.我试图追查过去2天的根本原因......请帮忙
编辑:有时我看到以下错误日志"PHP警告:exec():无法在第5行的/home/user/public_html/exect.php中分叉[php -l/tmp/PHPFileI4T43l]".我认为它是递归exec命令本身在每次递归时创建一个新进程,但无法得到它的原因.
aga*_*ggi 10
经过2天的头痛和大量的谷歌搜索......我在链接中找到了解决方案http://www.mombu.com/php/php-5-forum/t-24759-exec-or-system-et-all -cause-一个无限-环-的起动请求的程序-8469354.html
它是PHP CGI版本,它从环境中读取脚本名称,从而导致我的调用脚本无限次运行或直到最大允许进程数或直到整个内存被消耗.
简单地使用命令php-cli而不是命令php的解决方案.
我在我的代码中替换了以下行
exec("php -l ".$tmpfname,$error);
Run Code Online (Sandbox Code Playgroud)
同
exec("php-cli -l ".$tmpfname,$error);
Run Code Online (Sandbox Code Playgroud)
现在一切都很好.
我希望它能帮助别人. 我也改变了这个问题的标题,以便其他人可以轻松地在谷歌中找到相同问题的解决方案.