开发者

PHP Show CMD Command Prompt Window (Windows)

开发者 https://www.devze.com 2023-03-09 13:17 出处:网络
Usually, you would want to hide the CMD window. In my case, I\'ll be doing some tests with PHP+CLI. It will be mighty useful if PHP could be made to launch a 开发者_如何学CCLI popup.

Usually, you would want to hide the CMD window. In my case, I'll be doing some tests with PHP+CLI. It will be mighty useful if PHP could be made to launch a 开发者_如何学CCLI popup. But alas, I can't find a way to make it to.

The following command should have made it work, but it doesn't:

START "PHP AE2" CMD /K DIR

The PHP code in question is based on proc_open():

$proc=proc_open($cmd,array(
    0=>array('pipe','r'), // STDIN
    1=>array('pipe','w'), // STDOUT
    2=>array('pipe','w')  // STDERR
),$pipes);
// ....some stream stuff....
$return=proc_close($proc);

Although the command runs fine, the popup doesn't show up. I suspect this is a feature of Apache. Is there a way to completely detach the CMD from its parent?

Also, I'll be substituting DIR with a small script/batch/program that initializes the environment, so it is important that I can execute a program after the popup shows up.

(This is mostly for educational purposes and won't follow into production.)


I am bit late to answering this question, but to help close this question here is my answer.

The command is correct but you need to get Apache to interact with the desktop. If you are on a windows machine, just go to services, find Apache and make sure you tick the "Allow Service to Interact with desktop" checkbox.


Here is a project that allows PHP to obtain and interact dynamically with a real cmd terminal. Get it here: https://github.com/merlinthemagic/MTS

After downloading you would simply use the following code:

//if you prefer Powershell, replace 'cmd' with 'powershell'
$shellObj    = \MTS\Factories::getDevices()->getLocalHost()->getShell('cmd');

//you might need to ad php to the system environment variable
$strCmd1   = 'START "seq" cmd /c "PHP AE2"';
$return1   = $shellObj->exeCmd($strCmd1);

You can issue any command you like against the $shellObj, the environment is maintained throughout the life of the PHP script. So instead of bundling commands in a script file, just issue them one by one using the exeCmd() method, that way you can also handle the return and any exceptions.

0

精彩评论

暂无评论...
验证码 换一张
取 消