How to run php script that executes the .bat file? - php

I facing error while running the below php script. My php script having the little code for executing the .bat file which is located on my local server.
PHP file code : -
<?php
system("cmd /c C:\Users\my_computer_name\Desktop\sample.bat");
?>
sample.bat file code : -
#echo off
title This is your first batch script!
echo Welcome to batch scripting!
start calc.exe
When i remove start calc.exe from .bat file code it works fine.
Thank you in Advance....

Related

Issues executing python script from HTML button using PHP

I cannot get a button on my webpage to execute a python3 script that is also inside the same folder in the server as the rest of the html and php files.
exec();
system();
escapeshellcmd();
shell_exec()
all of these commands are not working for me right now. I have chmod +x my .py file and included #!/usr/bin/env python3 at the very beginning of my python file.
<?php
if( isset($_POST['runScript'])){
$command = escapeshellcmd('/nfs/nfs7/home/team51/cgi-
pub/dataProcess.py');
$output = shell_exec($command);
echo $output;
}
?>
<form method="post">
<input type="submit" name="runScript" value="runScript">
</form>
In the end the python script should place a csv file in the same folder as the rest of the files. But I am getting nothing.
Some functions just print back "Array()"
You are running pythin script from apache user. Python path may not be accessible by apache user
Use full python installation path to execute script.
exec("/usr/bin/python /nfs/nfs7/home/team51/cgi-
pub/dataProcess.py");
To find out exact error check apache error log. If error logs give permission denied , it means apache is not having permission to execute script.
You can get access from sudoers file.
Try Make a sh file to execute the python script and use PHP to execute this sh file

Run bat file which contains powershell script using php?

I want to run bat file via php.This bat file contains power shell script.I am able to run this via cmd but not via php.
AwsExecute.bat is,
powershell -command ./AWSExecuter.ps1
This AwsExecuter.ps1 which is inside the C:\AWS\Distributed-setup-3 folder.
In php I have tried like this,
Trail.php which is in http://localhost/performance folder.
<?php
exec('C:\AWS\Distributed-setup-3\AwsExecute.bat');
echo "Done";
?>
As well as,
<?php
exec('c:\WINDOWS\system32\cmd.exe /c START C:\AWS\Distributed-setup-3\AwsExecute.bat');
echo "Done";
?>
Both are not working here, I tried to another bat file which just creates a php file it works fine.
That is First.bat,
echo off
break>"C:\Users\Vidya\Desktop\performance.php"
echo done
Try running system
system("cmd /c C:\AWS\Distributed-setup-3\AwsExecute.bat");
For more on system read this

Python script not executing properly from Php

I'm trying to run a python script with Php but it's not running properly when I launch it with Php. This script, here "bot.py" interracts with the instagram api and writes the output as a text file in 'bot1.txt'.
Here is the Php code :
shell_exec("python automation/bot.py");
And here is the python snippet that writes in the file :
with open('../botlog/bot1.txt', 'a') as the_file:
the_file.write('Followed')
My problem is that when I launch it with Php the script runs but doesn't write in the file bot1.txt. When I run the python script with the command line everything works perfectly so I don't understand what's the deal here.
I'm working with MAMP on a local server.
Thank you !
Maybe you should specify your path to php.
$myPHPScriptPath = realpath(dirname(__FILE__));
Then specify the relative path to your python script.
Hope it helps

When i execute batch file from .php how can i actually open Windows Command Line in the foreground and see the entire process

I am going crazy trying to find how how i can start batch file from PHP script in the foreground. By Foreground i mean, to actually start cmd Windows and see the entire process. I don't even need to pass anything to this batch file, just simulate as if you have double-clicked on the batch file from php webpage. Please help.
try
<?php
system("cmd /c [path to file]");
?>
OR try
<?php
exec("[path to file]");
?>

exec not working in php script ( WAMP Server )

I am using WAMP server on my system to execute php scripts.
I want to execute a script test.php from my main script main.php.
For that I am using exec function like this exec('php test.php');. In test.php I have given one echo statement.
But when I run my main.php script from the browser I am not able to see output of test.php script.
What am I doing wrong ? please suggest.
You have to give the proper path of php.exe
exec("c:\wamp\<where ever you exe is>/php.exe test.php");
so it has to be a proper path
use this command
echo exec('php test.php', $output); //this will print your echo statement.
print_r($output);

Categories