你如何从PHP运行.bat文件?

时间:2022-01-29 16:02:23

Can anyone tell me how to execute a .bat file from a PHP script?

谁能告诉我如何从PHP脚本执行.bat文件?

I have tried:

我努力了:

exec("C:\[path to file]");
system("C:\[path to file]");

Nothing is working. I've checked the PHP manuals and googled around but can't find a good answer. Anyone know where I'm going wrong?

没有什么工作。我检查了PHP手册并用Google搜索,但找不到一个好的答案。谁知道我哪里出错了?

I'm running Windows 2003 Server and have successfully manually run the .bat file and it does what I need it to; I just need to be able to launch it programatically.

我正在运行Windows 2003 Server并成功手动运行.bat文件,它完成了我需要的工作;我只需要能够以编程方式启动它。

5 个解决方案

#1


46  

You might need to run it via cmd, eg:

您可能需要通过cmd运行它,例如:

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

#2


9  

<?php
exec('c:\WINDOWS\system32\cmd.exe /c START C:\Program Files\VideoLAN\VLC\vlc.bat');
?>

#3


6  

<?php
 pclose(popen("start /B test.bat", "r")); die();
?> 

#4


6  

When you use the exec() function, it is as though you have a cmd terminal open and are typing commands straight to it.

Use single quotes like this $str = exec('start /B Path\to\batch.bat');
The /B means the bat will be executed in the background so the rest of the php will continue after running that line, as opposed to $str = exec('start /B /C command', $result); where command is executed and then result is stored for later use.

当您使用exec()函数时,就好像您打开了一个cmd终端并直接输入命令。使用像$ str = exec这样的单引号('start / B Path \ to \ batch.bat'); / B表示bat将在后台执行,所以php的其余部分将在运行该行后继续,而不是$ str = exec('start / B / C command',$ result);执行命令的位置,然后存储结果供以后使用。

PS: It works for both Windows and Linux.
More details are here http://www.php.net/manual/en/function.exec.php :)

PS:它适用于Windows和Linux。更多细节请访问http://www.php.net/manual/en/function.exec.php :)

#5


0  

on my windows machine 8 machine running IIS 8 I can run the batch file just by putting the bats name and forgettig the path to it. Or by putting the bat in c:\windows\system32 don't ask me how it works but it does. LOL

在我的Windows机8运行IIS 8的机器上我可以运行批处理文件,只需将bats名称和forgettig路径放入其中即可。或者将蝙蝠放在c:\ windows \ system32中,不要问我它是如何工作的,但确实如此。大声笑

$test=shell_exec("C:\windows\system32\cmd.exe /c $streamnumX.bat");

$ test = shell_exec(“C:\ windows \ system32 \ cmd.exe / c $ streamnumX.bat”);

#1


46  

You might need to run it via cmd, eg:

您可能需要通过cmd运行它,例如:

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

#2


9  

<?php
exec('c:\WINDOWS\system32\cmd.exe /c START C:\Program Files\VideoLAN\VLC\vlc.bat');
?>

#3


6  

<?php
 pclose(popen("start /B test.bat", "r")); die();
?> 

#4


6  

When you use the exec() function, it is as though you have a cmd terminal open and are typing commands straight to it.

Use single quotes like this $str = exec('start /B Path\to\batch.bat');
The /B means the bat will be executed in the background so the rest of the php will continue after running that line, as opposed to $str = exec('start /B /C command', $result); where command is executed and then result is stored for later use.

当您使用exec()函数时,就好像您打开了一个cmd终端并直接输入命令。使用像$ str = exec这样的单引号('start / B Path \ to \ batch.bat'); / B表示bat将在后台执行,所以php的其余部分将在运行该行后继续,而不是$ str = exec('start / B / C command',$ result);执行命令的位置,然后存储结果供以后使用。

PS: It works for both Windows and Linux.
More details are here http://www.php.net/manual/en/function.exec.php :)

PS:它适用于Windows和Linux。更多细节请访问http://www.php.net/manual/en/function.exec.php :)

#5


0  

on my windows machine 8 machine running IIS 8 I can run the batch file just by putting the bats name and forgettig the path to it. Or by putting the bat in c:\windows\system32 don't ask me how it works but it does. LOL

在我的Windows机8运行IIS 8的机器上我可以运行批处理文件,只需将bats名称和forgettig路径放入其中即可。或者将蝙蝠放在c:\ windows \ system32中,不要问我它是如何工作的,但确实如此。大声笑

$test=shell_exec("C:\windows\system32\cmd.exe /c $streamnumX.bat");

$ test = shell_exec(“C:\ windows \ system32 \ cmd.exe / c $ streamnumX.bat”);