5

In the last days i was tying to code something in PHP to popup a CMD windows and this CMD windows have a command like "ping google.com" and process it
i don't need the PHP code to read the result , i only want it to run it i tried some thing like

<?php     
exec('C:\Windows\System32/cmd.exe ping google.com');     
<?

but no result (i don't know if it run it in background )
so i read this and i founded many ways but nothing worked

My OS is windows and thanks alot for all :)

user3421990
  • 161
  • 1
  • 1
  • 7
  • First up: your path is all wrong: `C:\Windows` should be `C:\\Windows\\System32` and the `System32/cmd.exe` should be `System32\\cmd.exe`. Forward slashes should work on windows, too, but don't mix them together, be consistent. Also: where will the PHP script run, and where do you want the prompt to show up? – Elias Van Ootegem Nov 13 '14 at 12:35
  • @EliasVanOotegem Can u explain more ? like what is the correct command ? – user3421990 Nov 13 '14 at 12:37
  • 1
    I can't, not yet. There's not enough information to go on here... I don't know how you're planning to run this PHP script, and what you expect to see. If you run this code on your machine, simply doing `exec('ping 8.8.8.8');` should open a cmd window anyway, whereas `exec('start /B ping 8.8.8.8', $status, $result);` starts the process in the background, leaving you to process the output that'll be assigned to `$result`, and the exit code is found in `$status` – Elias Van Ootegem Nov 13 '14 at 12:40
  • Looking at your previous questions, you seem to think that people here will write code for you. That's not what this site is about: either you learn to code, or you pay someone to do it for you. This isn't a code-request site, it's a problem solving Q&A site for people who are willing to do the work themselves – Elias Van Ootegem Nov 13 '14 at 12:43
  • Related, [How do you run a .bat file from PHP?](http://stackoverflow.com/q/835941) – jww Nov 15 '16 at 04:03

3 Answers3

11

the answer is
i searched alot to find it :))

<?
execInBackground('start cmd.exe @cmd /k "ping google.com"');



function execInBackground($cmd) { 
    if (substr(php_uname(), 0, 7) == "Windows"){ 
        pclose(popen("start /B ". $cmd, "r"));  
    } 
    else { 
        exec($cmd . " > /dev/null &");   
    } 
}
?>
user3421990
  • 161
  • 1
  • 1
  • 7
  • popen('start /D "'.getenv("PHPRC").'" /B ' . $exec_string, "r"); to set the Working Directory, avoiding DLL loading issues – IanMcL Oct 08 '18 at 14:52
0

How about a real command prompt? 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');

$strCmd1   = 'ping -n 4 www.google.com';
$return1   = $shellObj->exeCmd($strCmd1);

The return will give you the command return OR error from cmd, just as if you sat at the console.

Furthermore, 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.

MerlinTheMagic
  • 575
  • 5
  • 16
0

it is simple as shell_exec('ping google.com'), for opening a new window you can use the following command

shell_exec('start cmd.exe @cmd /k "ping google.com"')

this will open a new prompt window, even you can enter multiple commands

shell_exec('start cmd.exe @cmd /k "ping google.com & ping facebook.com"')
MANSOOR KOCHY
  • 370
  • 2
  • 9