How to run cmd commands as admin from php

I am trying to learn the php function shell_exec (), which, it seems to me, allows you to run cmd commands from a php script. But on my system, when I usually open a command prompt and type a command, for example ipconfig, it says:

'ipconfig' is not recognized as an internal or external command, operable program or batch file.

So, to avoid this, I have to right-click on the command line and select "run as administrator", and everything will be fine.

Now my problem is that I'm trying to run similar commands like ipconfiginside a PHP script, I get the same error as the one above. Is there any way to solve this problem? I mean, is there a way to run cmd commands as administrator from inside php?

I am enclosing the sample code below so that you can edit it and help me. Thanks in advance.

Here is the code:

<?php
$output = shell_exec('ipconfig 2>&1');
echo $output;
?>

Exit (which I want to avoid)

'ipconfig' is not recognized as an internal or external command, operating program, or batch file.

+4
source share
1 answer

Right-click the command prompt and select "run as administrator"

I assume that you are using the XAMPP and defualt installation location. If not serach the php.exe file and use this full path.

C:\xampp\php\php.exe  full\path\file.php

Therefore, you can run php script with administrator privileges.

0
source

All Articles