PHP exec to launch a cmd window with parameters

2019-02-26 12:07发布

问题:

I have been trying to launch a cmd window using the php exec method. The idea is to launch cmd and get it to start running a file. My first problem was being able to rewrite the bat file on the PC when user changes the information such as their user information or what tools they would like to launch. Now that is no longer a problem. I was able to use php popen function to get passed that. From all the reading I have done I was able to determine that it is possible to launch cmd with an additional command telling it to launch the batch file. I just need a good example so I can wrap my head around it. If anybody can help I would really appreciate it.

Thanks in advance

回答1:

Make sure the batch file is executable, then you can simply exec it:

exec('myscript.bat');

You shouldn't need to do it via cmd. If you want to for some reason, you will have to provide extra parameters just as you would on the command line:

exec('cmd /C myscript.bat');

http://ss64.com/nt/cmd.html

will show you how to provide other options to cmd (and explain the one above).

Have fun, let me know if you get any problems.