Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP+;命令行执行_Php_Command Line_Exec - Fatal编程技术网

PHP+;命令行执行

PHP+;命令行执行,php,command-line,exec,Php,Command Line,Exec,我正在尝试从命令行使用PHP运行一个.bat文件。我正在使用Windows Vista Home Premium 当我在像ipconfig.exe这样的文件上使用脚本时,我会得到输出。但是,当我运行.bat文件时,它会输出文件中的内容,但不会执行它 以下内容有效,并为我提供输出: $runCommand = "C:\\WINDOWS\\system32\\ipconfig.exe"; $WshShell = new COM("WScript.Shell"); $output = $WshShel

我正在尝试从命令行使用PHP运行一个.bat文件。我正在使用Windows Vista Home Premium

当我在像ipconfig.exe这样的文件上使用脚本时,我会得到输出。但是,当我运行.bat文件时,它会输出文件中的内容,但不会执行它

以下内容有效,并为我提供输出:

$runCommand = "C:\\WINDOWS\\system32\\ipconfig.exe";
$WshShell = new COM("WScript.Shell");
$output = $WshShell->Exec($runCommand)->StdOut->ReadAll;
echo "<p>$output</p>";
如果复制并粘贴到Windows命令行中,则此命令将成功执行


不知道发生了什么事。请提供帮助。

这是因为bat文件是一个排队的命令列表,用于提示。请尝试以下操作:

cmd/c myfile.bat

(也可能是/k,忘记执行和关闭哪个)

另外,副本

编辑

<?php
  // http://www.php.net/manual/en/function.exec.php#85930

  $_ = null;

  // If you care about the return value, use this:
    passthru("C:\\WINDOWS\\system32\\cmd.exe /c custom.bat",$_);
    header('Content-Type: text/plain');
    echo $_;
  // if you don't care, just use this:
    $_ = exec("C:\\WINDOWS\\system32\\cmd.exe /c custom.bat");
?>

12个问题,1个接受答案。请阅读常见问题解答:我将C:\\Temp\\foo.bat替换为C:\\WINDOWS\\system32\\cmd.exe/k C:\\Temp\\foo.bat,但它仍然没有执行。请参阅我在回答中提到的链接,该链接指向与批处理文件和php相关的其他问题。他们在接受的答案中有一个很好的例子。我尝试了exec('C:\WINDOWS\system32\cmd.exe/C START C:\Temp\foo.bat',$out),但它使我的Apache服务器崩溃。不知道你是怎么做到的?!请看我的最新答案。另外,请确保将其标记为已接受,因为我已经为您搜索了Google和RTM'n;passthru(“C:\\WINDOWS\\system32\\cmd.exe/C:\\Temp\\foo.bat”,$);标题(“内容类型:文本/普通”);回声$\;仍然不起作用。
C:/windows/system32/schtasks.exe /create /tn "TestTask" /tr "C:/Temp/configure.php" /sc minute /st 08:00:00
<?php
  // http://www.php.net/manual/en/function.exec.php#85930

  $_ = null;

  // If you care about the return value, use this:
    passthru("C:\\WINDOWS\\system32\\cmd.exe /c custom.bat",$_);
    header('Content-Type: text/plain');
    echo $_;
  // if you don't care, just use this:
    $_ = exec("C:\\WINDOWS\\system32\\cmd.exe /c custom.bat");
?>