PHP +命令行执行

发布于 2024-10-06 08:27:20 字数 831 浏览 0 评论 0原文

我正在尝试从命令行使用 PHP 运行 .bat 文件。我使用的是 Windows Vista 家庭高级版。

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

下面的内容可以工作并给我输出:

$runCommand = "C:\\WINDOWS\\system32\\ipconfig.exe";
$WshShell = new COM("WScript.Shell");
$output = $WshShell->Exec($runCommand)->StdOut->ReadAll;
echo "<p>$output</p>";

但这不行:

$runCommand = "C:\\Temp\\foo.bat";
$WshShell = new COM("WScript.Shell");
$output = $WshShell->Exec($runCommand)->StdOut->ReadAll;
echo "<p>$output</p>";

这是我的 foo.bat 文件中的内容:

C:/windows/system32/schtasks.exe /create /tn "TestTask" /tr "C:/Temp/configure.php" /sc minute /st 08:00:00

如果我复制此内容并粘贴到我的 Windows 命令行中,则该命令将成功执行。

不知道发生了什么事。请协助。

I'm trying to run a .bat file using PHP from the command-line. I'm using Windows Vista Home Premium.

When I use the script on a file like ipconfig.exe, I get the output. However, when I run the .bat file, it gives me output of what is in the file but it doesn't execute it.

What is below works and give me output:

$runCommand = "C:\\WINDOWS\\system32\\ipconfig.exe";
$WshShell = new COM("WScript.Shell");
$output = $WshShell->Exec($runCommand)->StdOut->ReadAll;
echo "<p>$output</p>";

But this doesn't:

$runCommand = "C:\\Temp\\foo.bat";
$WshShell = new COM("WScript.Shell");
$output = $WshShell->Exec($runCommand)->StdOut->ReadAll;
echo "<p>$output</p>";

Here's what's in my foo.bat file:

C:/windows/system32/schtasks.exe /create /tn "TestTask" /tr "C:/Temp/configure.php" /sc minute /st 08:00:00

If I copy this and paste in my Windows Command Line, this command executes successfully.

Not sure what is going on. Kindly assist.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海不蓝 2024-10-13 08:27:20

这是因为 bat 文件是提示命令的排队列表。尝试以下命令:

cmd /c myfile.bat

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

此外,复制如何从 PHP 运行 .bat 文件?

编辑

<?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");
?>

That's because a bat file is a queued list of commands for a prompt. Try the following:

cmd /c myfile.bat

(it may be /k too, forget which executes and closes)

Also, duplicate of How do you run a .bat file from PHP?

EDIT

<?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");
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文