php 如何通过PHP执行批处理文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19088379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:49:15  来源:igfitidea点击:

How to execute batch file via PHP?

phpbatch-filecommand-lineexec

提问by Nikunj K.

I tried to execute the batch file using exec command in PHP. I just used it like:

我尝试在 PHP 中使用 exec 命令执行批处理文件。我只是像这样使用它:

$filename = 'test.bat';
exec($filename);

But didn't get any output. I tried this function with another command, it works fine. Your suggestions would be highly appreciated. Thanks

但没有得到任何输出。我用另一个命令尝试了这个功能,它工作正常。您的建议将不胜感激。谢谢

回答by Nikunj K.

The main issue was of pathand permission. I have gotten my batch file to execute.

主要问题是pathpermission。我已经得到了我的批处理文件来执行。

Here is my solution:

这是我的解决方案:

  1. I run my batch file from the same folder the php file is in.

    exec("mybatch.bat");

  2. I make sure that Apache Service has enough permission to run the batch file. Just to test i used an administrator account for Apache to log on with.

  1. 我从 php 文件所在的同一文件夹运行我的批处理文件。

    exec("mybatch.bat");

  2. 我确保 Apache 服务有足够的权限来运行批处理文件。只是为了测试我使用 Apache 的管理员帐户登录。

回答by Milan

On Windowsserver mind the quotes. This is what works for me:

Windows服务器上注意引号。这对我有用:

system('cmd.exe /c C:\myfolder\_batches\run_this_batch.bat');

回答by Mark Giblin

What I did was the following:

我所做的是以下内容:

  1. created a PHP file that contained :

    $gotIt = array();
    $file = "getMyIP.bat";
    exec( $file, $gotIt );
    echo implode("<br>",$gotIt);
    
  2. Created a batchfile in the same folder

    @ECHO off
    ipconfig
    
  3. Ran it and waited for the firewall to jump all over the action.

  1. 创建了一个包含以下内容的 PHP 文件:

    $gotIt = array();
    $file = "getMyIP.bat";
    exec( $file, $gotIt );
    echo implode("<br>",$gotIt);
    
  2. 在同一文件夹中创建了一个批处理文件

    @ECHO off
    ipconfig
    
  3. 运行它并等待防火墙跳过所有动作。

I then got an output like :

然后我得到了一个输出:

Windows IP Configuration


PPP adapter 3 USB Modem:

Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : ***.***.202.81
Subnet Mask . . . . . . . . . . . : 255.255.255.255
Default Gateway . . . . . . . . . : ***.***.202.81

only theres numbers where the ***'s are

只有 *** 所在的数字

回答by Ruddy

system("cmd /c C:[path to file]");

As "RichieHindle" said in a similar topic.

正如“RichieHindle”在类似话题中所说的那样。

or try

或尝试

exec("cmd.exe /c test.bat") ?

回答by Katran

If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.

如果您需要执行一个命令并且让命令中的所有数据直接传回而不受任何干扰,请使用 passthru() 函数。

http://md1.php.net/manual/en/function.passthru.php

http://md1.php.net/manual/en/function.passthru.php

回答by fluminis

As it is explained in the exec doc:

正如exec 文档中所解释的:

echo exec($filename);

or

或者

exec($filename, $output);
echo $output;