简体   繁体   English

无法让.bat在WAMP本地主机上与PHP一起运行

[英]Can't get .bat to run with PHP on WAMP localhost

I've been busting my head against the wall on this one for hours and I can't make any progress. 我已经把头撞在墙上的墙上好几个小时了,我什么也做不了。

Right now I've simmered down my problem to the simple (maybe?) task of trying to get a PHP script to run a .bat program, on localhost. 现在,我已经将问题简化到了一个简单的(也许是?)任务,该任务试图让PHP脚本在本地主机上运行.bat程序。 I'm using WAMP. 我正在使用WAMP。

This is my current PHP file: 这是我当前的PHP文件:

<?php system('cmd /c C:\\Users\\user\\Desktop\\open.bat'); ?>

That's all there is in it. 这就是全部。 My .bat file, which is on the desktop, contains: 我的.bat文件位于桌面上,包含:

notepad.exe C:\\Users\\user\\Desktop\\test.txt

And then I have a simple text file called test.txt on the desktop. 然后,我在桌面上有一个名为test.txt的简单文本文件。 I can run the .bat file from the command line and it works fine, but nothing from within the PHP. 我可以从命令行运行.bat文件,它可以正常工作,但PHP内没有任何帮助。

There's numerous other threads here asking how to run a .bat from PHP (ex: How do you run a .bat file from PHP? ), and I've tried pretty much every technique I've read about online, and NOTHING WORKS. 这里还有许多其他线程在问如何从PHP运行.bat(例如: 如何从PHP 运行.bat文件? ),并且我已经尝试了几乎所有在线学习的技术,并且没有任何工作。

Ex, I've tried 例如,我尝试过

exec('open.bat') (with the .bat in the same directory), shell_exec() , changing around the locations of files, paths, I really don't know what's up with it. exec('open.bat')位于同一目录中), shell_exec() ,在文件,路径的位置周围变化,我真的不知道这是怎么回事。 I'm not running PHP in safe mode. 我没有在安全模式下运行PHP。

Perhaps there's some configuration that I should know about that will allow it to run? 也许我应该知道一些配置,使其可以运行? Or maybe I'm missing something painfully obvious... 也许我错过了明显痛苦的事情...

您可能看不到“记事本”运行,因为它是一个交互式程序,并且Web服务器(正在尝试启动它)在与所在的会话不同的用户会话中运行。 php -f )在这种情况下可以工作。

you could try using start /b C:\\Users\\user\\Desktop\\open.bat instead of cmd 您可以尝试使用start /b C:\\Users\\user\\Desktop\\open.bat而不是cmd

also you might have to escape the slashes, like so \\\\ instead of \\ 也可能需要转义斜线,例如\\\\而不是\\

So, I suggest you try: 因此,我建议您尝试:

<?php system('start /b C:\\\\Users\\\\user\\\\Desktop\\\\open.bat'); ?>

Try This.... 尝试这个....

<?php
$cmd = C:\Users\user\Desktop\open.bat;
function execInBackground($cmd) {
   if (substr(php_uname(), 0, 7) == "Windows"){
       pclose(popen("start /B ". $cmd, "r")); 
   }
   else {
       exec($cmd . " > /dev/null &");  
   }
}
execInBackground($cmd);
?>

For more refer: http://php.net/manual/en/function.exec.php 有关更多信息,请参见: http : //php.net/manual/zh/function.exec.php

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

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