Windows 2003上的PHP 5.2.13
我正在使用DOS Start/B命令来使用PHP popen()函数启动后台应用程序:
popen("start /B {$_SERVER['HOMEPATH']}/{$app}.exe > {$_SERVER['HOMEPATH']}/bg_output.log 2>&1 & echo $!", 'r');
popen()函数启动一个cmd.exe进程,该进程运行指定的命令;但是,如果命令失败(例如,在上述示例中{$ app} .exe不存在或被锁定),则cmd.exe进程将永远不会返回,并且PHP将无限期挂起。
直接使用命令提示符调用失败的DOS命令会导致错误提示,需要单击“确定”按钮。
我认为此错误确认要求是阻止cmd.exe进程从命令提示符(使用CGI和CLI)和Web(使用带Apache 2.2的Apache 2.0处理程序)返回到PHP的原因。
有没有一种方法可以编写DOS命令或配置服务器或cmd.exe应用程序本身,以将DOS错误返回到原始调用而不是等待确认?
最佳答案
从手册:http://php.net/manual/en/function.popen.php
If the command to be executed could not be found, a valid resource is returned. This may seem odd, but makes sense; it allows you to access any error message returned by the shell:
error_reporting(E_ALL);
/* Add redirection so we can get stderr. */
$handle = popen('/path/to/executable 2>&1', 'r');
echo "'$handle'; " . gettype($handle) . "\n";
$read = fread($handle, 2096);
echo $read;
pclose($handle);
关于php - 优美的DOS命令错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2580897/