开发者

PHP + Command Line Execution

开发者 https://www.devze.com 2023-01-29 02:50 出处:网络
I\'m trying to run a .bat file using PHP from the command-line. I\'m using Windows Vista Home Premium.

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 Co开发者_JAVA技巧mmand Line, this command executes successfully.

Not sure what is going on. Kindly assist.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号