你如何从 PHP 运行 .bat 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/835941/
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
How do you run a .bat file from PHP?
提问by undefined
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 手册并在谷歌上搜索,但找不到好的答案。有谁知道我哪里出错了?
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 文件,它完成了我需要的工作;我只需要能够以编程方式启动它。
回答by RichieHindle
You might need to run it via cmd, eg:
您可能需要通过 运行它cmd,例如:
system("cmd /c C:[path to file]");
回答by mysoogal
<?php
exec('c:\WINDOWS\system32\cmd.exe /c START C:\Program Files\VideoLAN\VLC\vlc.bat');
?>
回答by Migisha
When you use the exec()function, it is as though you have a cmdterminal open and are typing commands straight to it.
Use single quotes like this $str = exec('start /B Path\to\batch.bat');
The /Bmeans the bat will be executed in the background so the rest of the phpwill continue after running that line, as opposed to $str = exec('start /B /C command', $result); where commandis executed and then resultis stored for later use.
当您使用该exec()功能时,就好像您cmd打开了一个终端并直接向其输入命令。
使用单引号是这样$str = exec('start /B Path\to\batch.bat');
的/B,蝙蝠会在后台执行,因此其余手段PHP将运行线后继续进行,而不是$str = exec('start /B /C command', $result); 在那里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:)
回答by testaa
<?php
pclose(popen("start /B test.bat", "r")); die();
?>
回答by calmchess
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
在运行 IIS 8 的 Windows 机器 8 机器上,我只需输入 bat 名称并忘记它的路径即可运行批处理文件。或者通过将 bat 放入 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");
回答by Alex Khimich
This snippet is from working code.
此代码段来自工作代码。
You can trigger bat file not only from Windows GUI or Task Scheduler, but directly from PHP script when you need it. But in most cases it will have execution for 30-60 sec. depending from your PHP configuration. If job in BAT file is long and you don't want to freeze your PHP scripts, you need to fork BAT job as another process using php.exeand not be dependable from Apache.
您不仅可以从 Windows GUI 或任务计划程序触发 bat 文件,还可以在需要时直接从 PHP 脚本触发。但在大多数情况下,它将执行 30-60 秒。取决于您的 PHP 配置。如果 BAT 文件中的作业很长,并且您不想冻结 PHP 脚本,则需要将 BAT 作业分叉为另一个进程使用,php.exe并且不依赖 Apache。
This runs in backgroundmode in Windows, seen as separate processes cmd.exeand php.exefrom Task Manager not halting your Apache PHP scripts. The messages produced by your script may be stored and retrieved back via log files.
这在运行后台模式在Windows中,被视为独立的进程cmd.exe,并php.exe从任务管理器不停止Apache的PHP脚本。您的脚本生成的消息可以通过日志文件存储和检索。
In my case in file_scanner.phpI do some heavy calculations in loop for big array of files which may last for few hours with php function sleep()not to overload CPU.
就我而言,file_scanner.php我在循环中对大文件数组进行了一些繁重的计算,这些文件可能会持续几个小时,而 php 函数sleep()不会使 CPU 过载。
The success poiner result from file $rwhich you can query via ajax if you want to know success or fauty start. In my case file_scanner.phpwrites log file with messages somefile.jpg - OKwich you can load to your UI with AJAX every few seconds to show progress.
成功指针结果来自文件 $r,如果您想知道成功或错误启动,您可以通过 ajax 查询。在我的情况下,file_scanner.php写入带有消息的日志文件, somefile.jpg - OK您可以每隔几秒钟使用 AJAX 加载到您的 UI 以显示进度。
PHP
PHP
/**
* Runs bat file in background mode
*
*/
function run_scanner() {
$c='start /b D:\Web\example.com\tasks\file_scanner.bat';
$r=pclose(popen($c, 'r'));
return json_encode(array('result'=>$r));
}
BAT
蝙蝠
@echo Off
D:\PHP\php.exe D:\Web\example.com\tasks\file_scanner.php > D:\Web\example.com\tasks\file_scanner.log
exit

