如何使用PHP运行批处理文件?

时间:2018-04-23 07:15:34

标签: php batch-file xampp wamp

如何在wamp服务器中执行批处理文件?在Xampp服务器上,我得到了正确的结果,但在wamp服务器上,获得空结果..

$gotIt = array();
$file111 = "C:/ABC/run.bat";
//echo $file; exit;
$kkk =  exec( $file111, $gotIt );
//echo $kkk;
$usr = implode("",$gotIt);
echo '<pre>'; print_r($usr);exit;

1 个答案:

答案 0 :(得分:0)

你试过这个吗?

system("cmd /C X:[PATH_TO_BAT_FILE]", &$output);

其中参数

  

/ C =执行字符串指定的命令,然后终止

     

&amp; $ output =返回值

在您的情况下

$gotIt = array();
$file111 = "C:/ABC/run.bat";

/** CMD cant return directly Array, but Text */
$CMDOutput = "";
system("cmd /C \"$file111\"", $CMDOutput);

/** Split output as you want (With Token, with preg_match, ...) */
$gotIt = mySplitFunction($CMDOutput);

/** 
 * EX. with explode where first param is a token (delimiter)
 *
 * If $CMDOutput contains "Pietro, terracciano", with this
 * instruction you obtain 
 *     $gotIt = array(
 *         'Pietro', 'Terracciano'
 *     );
 */
$gotIt = explode(',', $CMDOutput);

/** ... */