简体   繁体   English

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

[英]How to execute batch file via PHP?

I tried to execute the batch file using exec command in PHP. 我尝试在PHP中使用exec命令执行批处理文件。 I just used it like: 我只是用它:

$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 谢谢

The main issue was of path and permission . 主要问题是pathpermission I have gotten my batch file to execute. 我已经让我的批处理文件执行了。

Here is my solution: 这是我的解决方案:

  1. I run my batch file from the same folder the php file is in. 我从php文件所在的同一个文件夹中运行我的批处理文件。

    exec("mybatch.bat");

  2. I make sure that Apache Service has enough permission to run the batch file. 我确保Apache Service具有足够的权限来运行批处理文件。 Just to test i used an administrator account for Apache to log on with. 只是为了测试我使用Apache的管理员帐户登录。

On Windows server mind the quotes. Windows服务器上注意引号。 This is what works for me: 这对我有用:

system('cmd.exe /c C:\myfolder\_batches\run_this_batch.bat');
system("cmd /c C:[path to file]");

As "RichieHindle" said in a similar topic. 正如“RichieHindle”在类似话题中所说的那样。

or try 或尝试

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

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

What I did was the following: 我做的是以下内容:

  1. created a PHP file that contained : 创建了一个PHP文件,其中包含:

     $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. 跑吧,等待防火墙跳过整个行动。

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 只有***的数字

As it is explained in the exec doc : 正如exec doc中解释的那样:

echo exec($filename);

or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM