PHP executing script in subfolder - php

I've run into a slight issue with the way my site works.
I have a python script which I want to run from the index.php page.
Now if this script is in the root directory, by using this command I can execute it easily:
<?php
exec("myscript.py");
?>
This actually runs the python script and does what is needed. However, I am planning to have a few more script and want to keep the root dir as clean as possible, hence I was wondering if it is possible to execute this script from a subdir within my root?
Current Setup:
Root - C:\wamp\www\homepage\
Python Script Folder - ...\homepage\python\
When i put my scripts in the python folder, no matter what I use, the php does not execute it.
Tried:
<?php
exec("/python/myscript.py")
?>
<?php
exec("//python//myscript.py")
?>
<?php
exec("\python\myscript.py")
?>
<?php
exec("\\python\\myscript.py")
?>
<?php
exec("python/myscript.py")
?>
<?php
exec("python\myscript.py")
?>
<?php
exec("../python/myscript.py")
?>
All this and nothing launches the darn thing. What am I doing wrong :[

Did you try calling the python executable directly? It worked for me. I had a myscript.py like this:
print 'hello'
print 'world'
and then put it in a directory called temp/.
My php is in a file called temp.php as follows:
<?php
exec('python temp/myscript.py',$output);
print_r($output);
?>
Then I ran it and here's what happened:
$ php temp.php
Array
(
[0] => hello
[1] => world
)

Related

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

how to run Python script from php

I want to run python script from php.
this is my python code. It is saved in /home/pi and name of file is hello.py
#! /usr/bin/python
import bluetooth
bd_addr="xx:xx:xx:xx:xx:xx"
port=1
sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((bd_addr.port))
data=""
while 1:
try:
data +=sock.recv(1024)
data_end=data.find('\n')
if data_end!=-1:
rec=data[:data_end]
print datas
data=data[data_end+1:]
except KeyboardInterrupt:
break
And here is my php code. It is saved in /var/www/html and name of file is php.php
<?php
$output=shell_exec('ls -l /home/pi/hello.py');
echo "<pre>$output</pre>";
?>
And I insert localhost/php.php in chrome, it displays
-rw-r-r- 1 pi pi 378 Mar 8 12:07 /home/pi/hello.py
what is the problem??
As pointed out by Jon Stirling, you are using "ls" to only listing the content of the folder or to check whether the file exist in that folder. To run the Python code, you need to change the PHP file into something like this:
<?PHP
$output=shell_exec('./hello.py');
echo "<pre>$output</pre>";
?>
ls command is used to list files in a directory or to get information about a file. You are ls-ing on your python file and the result is correct. It is providing you with information about the file.
Just put the file name inside of shell_exec that is /home/pi/hello.py. If you do not want to depend on the shebang and the command python is available in your shell environment then you can use python /home/pi/hello.py instead of bare /home/pi/hello.py.
Again, you used the variable datas with print where you intended to use data - fix it.
php code:
<?php
$output=shell_exec('python /home/pi/hello.py');
echo "<pre>$output</pre>";
?>
or:
<?php
$output=shell_exec('/home/pi/hello.py');
echo "<pre>$output</pre>";
?>
python code:
#! /usr/bin/python
import bluetooth
bd_addr="xx:xx:xx:xx:xx:xx"
port=1
sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((bd_addr.port))
data=""
while 1:
try:
data +=sock.recv(1024)
data_end=data.find('\n')
if data_end!=-1:
rec=data[:data_end]
print data
data=data[data_end+1:]
except KeyboardInterrupt:
break

problems with chdir in PHP script to call a shell script

I am trying to use a PHP script to call a shell script and print the output to the browser.
I have confirmed that all permissions settings are set correctly, both the PHP and the bash script have full control.
If I run the shell script in the current directory (same directory as the PHP file) then it works fine. But if I try to run the shell script from /some/other/directory/shell.script, then it does not work. Why is this so?
I have tried to chdir() to the other directory, but after running a getcwd(), it never changed directories.
I have also tried the exec command by itself and it does not work.
<?php error_reporting(E_ALL); ini_set('display_errors',1);
echo "<h1>EDS Count Report</h1><p>";
$output=shell_exec('cd /app/script/catalog/ && ./EDScount_report.sh');
echo $output;
?>
contents of SHELL script:
#!/bin/bash
echo "this is a test!"
Try this please
<?php
$output=shell_exec('sh /path/to/otherdirectory/shellscript.sh');
echo $output;
?>
if you are certain that you can access that dicertory , you can use also this:
<?php
$output=shell_exec('cd /path/to/otherdirectory/ && ./shellscript.sh');
echo $output;
?>

How to execute external PHP script in command line from your browser

I have an external php script named external.php which includes this:
<?php
echo 'External Output';
?>
When I run that on command line with command:
php external.php
I get output as
External Output
But when the same script I execute from my browser's php file named index.php which has this code:
<?php
$exe=exec('php external.php',$out,$ret);
print_r($out);
?>
Then I get no output.
When I modify it as:
<?php
$exe=exec('php external.php 2>&1',$out,$ret);
print_r($out);
?>
Then I get this output:
php: /opt/lampp/lib/libxml2.so.2: version `LIBXML2_2.9.0' not found (required by php)
My question is, how can I execute that "external.php" file in commandline from index.php and get output on my browser (i.e. on index.php) ? I have tried system() function too, it doesn't work as well.
Edit:
I cannot include the external.php in index.php because external.php can take lot of time to execute (more than 10 hours).
Edit#2
Solved it, giving the full path to PHP solved the problem.
Here is an example:
<?php
$exe=exec('full/path/to/php /full/path/to/external.php',$out,$ret);
print_r($out);
?>
Thankyou Oleg and Scopey for giving me some hint help.
Specify the full path of external.php:
<?php
$exe=exec('php /full/path/to/external.php',$out,$ret);
print_r($out);
?>
Try also specifying the full path to php on the command line and check that it works:
$/usr/bin/php "/full/path/to/external.php"
If that works, try it also in your php file:
<?php
$exe=exec('/usr/bin/php /full/path/to/external.php',$out,$ret);
print_r($out);
?>
Try running phpinfo() from the command line. Often installations of PHP use a different PHP.ini for CLI usage. Running phpinfo() will show you the location of the ini file in use. It appears as though this ini might not be configured properly for your installation (as a guess)

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