execute .bat file using php on web

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Shanker somanchi

shanker somanchi

04/01/2019 16:05:45
Quote Anchor link
hi all.

sorry for not writing in dutch. the reason I am in this forum is purely that I live in holland :-)
expecting some help from PHP experts.

started my first task in php.

1. i have a simple web application which must execute a .bat file.
2. when execution finished it shall display the results to users

i am using
xampp on windows 10.

the main issue is, due to system policy, i can only execute a .bat file in administrator mode.

Below code works:
=================
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
echo
exec('whoami');
exec('cd C:\softwares && dir >> temp.log');
//exec('dir');
$file = 'C:\softwares\temp.log';
$orig = file_get_contents($file);
$a = htmlentities($orig);

echo '<code>';
echo '<pre>';

echo $a;

echo '</pre>';
echo '</code>';

//echo file_get_contents( "c:\softwares\temp.log" );
?>


below code do not work :
========================
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
echo
exec('whoami');
exec('cd C:\softwares && testMyNameExists.bat >> temp.log');
//exec('dir');
$file = 'C:\softwares\temp.log';
$orig = file_get_contents($file);
$a = htmlentities($orig);

echo '<code>';
echo '<pre>';

echo $a;

echo '</pre>';
echo '</code>';

//echo file_get_contents( "c:\softwares\temp.log" );
?>


I understand that 'web user' should be run in Administrator mode. But i dont get which files has to be modified..
anyhelp is appreiciated. Thanks.
 
PHP hulp

PHP hulp

12/10/2024 15:37:26
 
Thomas van den Heuvel

Thomas van den Heuvel

04/01/2019 17:25:01
Quote Anchor link
Have you tried using more PHP-based commands to do whatever you are trying to do via a .bat file? In other words, what is the necessity to use a .bat file specifically? Sounds to me like you are trying to do something both via PHP and your system. Wouldn't it be more logical to either use the Windows scheduler or use a complete PHP-based approach?

And on that note, what are you trying to achieve through this .bat file. Perhaps there is another / better way to achieve whatever it is you are trying to do with said file.

Also (first Google result) have you tried to run a .bat file like this?
Gewijzigd op 04/01/2019 19:24:52 door Thomas van den Heuvel
 
Ben van Velzen

Ben van Velzen

05/01/2019 00:14:44
Quote Anchor link
Also, paths need to have \\ instead of \, otherwise the \ won't even show up.
 
Shanker somanchi

shanker somanchi

05/01/2019 13:53:35
Quote Anchor link
Hi Thomas and Ben.

Thanks for your tips.

the link has helped a little but I landed again at same situation as where I started. It executed the .bat file but i have issue with my system policy. I must run the bat file in 'admin' mode otherwise I am seeing following message.
"This program is blocked by group policy. For more information, contact your system administrator."

>what is the necessity to use a .bat file specifically?
mainly because It is a application from oracle and I am used to running it always on commandLine.
Now I want to let other users run the .bat and execute it too remotely. thus web application.

>Perhaps there is another / better way to achieve whatever
This may be a good idea but it shall test my newbie php skills :-)

by the way, >Also, paths need to have \\ instead of \, otherwise the \ won't even show up.
the double or single did not made difference in my tests.
 
Aad B

Aad B

05/01/2019 16:38:00
Quote Anchor link
Shanker somanchi op 05/01/2019 13:53:35:
the link has helped a little but I landed again at same situation as where I started. It executed the .bat file but i have issue with my system policy. I must run the bat file in 'admin' mode otherwise I am seeing following message.
"This program is blocked by group policy. For more information, contact your system administrator."
This is due to the (interactive use) architecture of windows. Run this on a unix or Linux system and you can control the proper rights for execution. You where saying: It is an application from oracle or something like that but Oracle has left windows many years ago so are you still running Oracle on windows??

However may be you will find some answers here:
https://stackoverflow.com/questions/18755553/automatically-running-a-batch-file-as-an-administrator using Powershell on windows
Gewijzigd op 05/01/2019 16:43:22 door Aad B
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.