Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run windows command in php

Tags:

php

system

exec

Is it possible to run windows command line code from php ? My windows command line code is:

<?php 
error_reporting(E_ALL); 
try {

echo exec('C:\xampp\mysql\bin>mysqlbinlog --start-datetime="2011-04-21 10:31:44" c:\xampp\mysql\data\binlog\bin-log.000001 > c:\xampp\mysql\data\binlog\sql.txt');

} catch (Exception $e) {
echo $e->getMessage();
}

Now I want to run this code from PHP using system() or exec() etc. any help appreciated.

like image 439
Shawon Avatar asked Jul 24 '13 09:07

Shawon


People also ask

Can PHP run shell command?

The shell_exec() function is an inbuilt function in PHP which is used to execute the commands via shell and return the complete output as a string. The shell_exec is an alias for the backtick operator, for those used to *nix.

How do I run a PHP command?

system() is just like the C version of the function in that it executes the given command and outputs the result. The system() call also tries to automatically flush the web server's output buffer after each line of output if PHP is running as a server module.

Can you run PHP on Windows?

PHP does not come pre-installed on Windows systems. To work with PHP on Windows, PHP will need to be manually downloaded and installed. You can download PHP from the PHP download page at http://www.php.net/downloads.php. Download the zip package from the "Windows Binaries" section.


2 Answers

If you are able to run your commands manually from the command-line, but not via WAMP, than the user the Apache Service runs as does not have the needed permissions to execute your commands and binaries (nor interact with the desktop if it is GUI related).

By default, Apache service run under user account: nt authority\system

Change it to your custom user (user with administrative rights) by following below steps,

  1. Hit Windows+R which opens Run
  2. Type "services.msc" which opens list of windows services
  3. Find and select your apache service and open its properties
  4. Go to Logon tab and change the account From “Local System” to another user/account
  5. Restart your Apache service

Happy coding :-)

like image 162
Loki Avatar answered Oct 12 '22 00:10

Loki


I'm using wamp, and the only solution was the followings:

At Control Panel / Administrative tools / Services, search for wampapache64, httpd, or something like that. On the Log On tab tick the 'allow service to interact with desktop'

Hope this helps!

like image 38
asmcod Avatar answered Oct 12 '22 00:10

asmcod