Reputation: 1030
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.
Upvotes: 14
Views: 61928
Reputation: 133
See if using scheduledtask works.
I had persisetnt difficulty calling a powershell script from PHP. PHP kept timing out (not all powershell commands; only some commands. Maybe it's got to do with security settings and how WAMP operates). The following worked as a workaround. Not elegant but it did the job. In your case you only want to run a normal non-powershell commandline command, which is going ot be even simpler and the same method should work.
As for me, I called the powershell script as a scheduledtask as follwing (in PHP):
$cmd = 'c:\wamp64\www\runme4.bat';
$cmd2 = 'SCHTASKS /F /Create /TN _notepad3 /TR "'.$cmd.'" /SC DAILY /RU INTERACTIVE';
shell_exec($cmd2);
shell_exec('SCHTASKS /RUN /TN "_notepad3"');
shell_exec('SCHTASKS /DELETE /TN "_notepad3" /F');
The reason I had to put the powershell command in a .bat was because there were too many double quatation marks. Single quotation marks would not work. This is the content of my runme4.bat (note thre is a space between the two dots. This poweshell command runs the ps1 script (the script is called AppointmentsToday.ps1) plus calls the function inside it. The function inside it is called Get-AppointmentsToday):
powershell -executionpolicy Bypass -noexit ". .\Get-AppointmentsToday.ps1;Get-AppointmentsToday"
More specifically the powershell script in my case was something like this (To look for today's appointments in both the detault and user-defined calendars on the local version of Outlook 2019. Fetch only subjects that contain hashtag or the word 'meeting'. Save the output result into a .txt file):
Set-ExecutionPolicy Bypass -Scope CurrentUser -Force
Function Get-AppointmentsToday
{
Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$olFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$folder = $namespace.getDefaultFolder($olFolders::olFolderCalendar)
$folder.items | Select-Object -Property Subject, Start
foreach($customfolder in $folder.Folders)
{
$customfolder.items | Select-Object -Property Subject, Start
}
} #end function Get-OutlookCalendar
$todayStart = Get-Date -Hour 0 -Minute 0 -Second 0
$todayEnd = Get-Date -Hour 23 -Minute 59 -Second 59
Get-AppointmentsToday | where-object { $_.start -gt $todayStart -AND $_.start -lt $todayEnd -AND ($_.subject -ilike "*#*" -OR $_.subject -ilike "*meeting*") } | Out-File -FilePath \wamp64\www\AppointmentsToday.txt
The code ran smoothly when I entered Powershell myself, or called it myself from commandline. However as soon as I tried to run it from PHP or as packaged in to an .EXE, the script failed to reach completion. PHP kept timing out. I removed all lines and then re-added them back in line by line. From memory as soon as I uncommented these two lines, the execution stalled: $folder = $namespace.getDefaultFolder($olFolders::olFolderCalendar) $folder.items | Select-Object -Property Subject, Start
The scheduledtask method worked for me. Hope my approach helps someone in a similar situation. You can also use this shedulestask method to run other .EXE applications if they fail to load using normal methods. I'd be glad to learn of a better way to do things.
Upvotes: 0
Reputation: 451
Run Command Prompt as administrator.
Traverse to the DOC_ROOT folder
cd C:\xampp\htdocs
Play with the below command,
php -S localhost:8080
If DOC_ROOT has different public folder
php -S localhost:8080 -t public public/index.php
It worked fine for me.
Make sure the php.exe is added to the environment path.
Upvotes: 0
Reputation: 383
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,
Happy coding :-)
Upvotes: 18
Reputation: 87
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!
Upvotes: 7
Reputation: 2032
C:\xampp\mysql\bin>mysqlbinlog
is not a command.
I think you mean C:\xampp\mysql\bin\mysqlbinlog
.
Notice the replacing of the >
in a \
.
The >
is only visible in the command prompt as a separator (for the eyes) but you should not use it like that in a command unless you are trying to redirect your output. (which you are doing again later in the line). So just replace that first >
in a \
and your command will run.
Upvotes: 0
Reputation: 1451
If you can't run the command directly in exec(), then what you can do is make a batch file with the command and place it on the root of your website. Then, just run:
<?php echo exec("script.bat"); ?>
Upvotes: 3
Reputation: 2536
Just give it a try. Or if you want a sandbox example just try to run
<?php echo exec("whoami");?>
Upvotes: 0