I am running a nginx web server, along with PHP-CGI.
I would like to know if it is possible to execute a Python script inside PHP pages, allowing both languages to be combined. I've attempted it briefly but it didn't work, so I'm not sure how I would do this. Here are the two files I used:
index.php
<body>
<p>Hello! Here is a message: <?php exec('python hello.py'); ?></p>
</body>
hello.py
print 'Hello World!'
Any clues would be appreciated.
exec will return the output of the shell command, but you still need to echo that to the page. The following code should work for you
<body>
<p>Hello! Here is a message: <?php echo exec('python hello.py'); ?></p>
</body>
Related
I'm running lighttpd 1.4.35 on Debian 8.2. I have a very simple html file with php code (php5) which calls a bash script and prints the output:
<html>
<body>
<?php
$res = shell_exec("/var/www/html/run.sh");
echo $res . " is the result";
?>
</body>
</html>
If that html file is called on firefox, the output is
is the result
If I directly run php with that file (php index.php), the output is
<html>
<body>
13.00
is the result</body>
</html>
So, where does the result get lost?
edit:
The webpage source code from firefox is
<html>
<body>
is the result</body>
</html>
edit: solved. bash script uses '~' which expands to wrong directory when the script is run from webserver.
The exec functions "only" return the contents of stdout, which is why you might miss an error message.
But you can redirect stderr to stdout, e.g. via
$res = shell_exec("/var/www/html/run.sh 2>&1");
shell_exec does not run if you're in safe mode, that could be the issue
This is my php script
<HTML>
<head>
<h1>Hello</h1>
</head>
<body>
<?php
$command=escapeshellcmd('~/PycharmProjects/Test_1/wiki_update.py 2 2>&1');
$output=shell_exec($command);
echo $output;
?>
</body>
</HTML>
I get no output when I run it through the browser.
When I change the
$command=escapeshellcmd('whoami');
it outputs as NOBODY,
I granted permission to nobody for that python script still no output.
I am using xampp for linux and php script in htdocs/xampp.
Are you sure you don't need to change your command to run python?
$command=escapeshellcmd('python ~/PycharmProjects/Test_1/wiki_update.py 2 2>&1');
You also might try specifying absolute path - the ~ alias changes based on current user (which is the web server's user, not yours).
So I have this code and I'm only trying to make a list of the saves in another directory where the php scrip is in xampp folder and the saves are to this path /root/files/saves:
<html>
<body>
<?php
$output = shell_exec('ls /root/files/saves');
echo "<pre>$output</pre>";
?>
</body>
</html>
I don't know why I can't get it working on a var_dump it seems output is null I'm really confuse it should work or I just it all wrong I need some help.
Add 2>&1 to the end of your shell command to have STDERR returned as well as STDOUT.
$output = shell_exec("ls /root/files/saves 2>&1");
Also, if the user running PHP doesn't have sufficient permissions to view the output in /root/, the above code will return a Permission denied error message.
Source: http://php.net/manual/en/function.shell-exec.php#28994
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
)
I am running through a weird problem with exec's in PHP. When I load in the browser the script x.php, the browser hangs and when I run a ps I can see multiple threads being created. The only way to stop them is by restarting apache.
However, instead of running a php script, if I do something like system('ls'), it works fine. So it seems to be problem when a PHP script tries to run another script using exec/system/passthru (I've tried them all).
x.php is defined as following:
<?php
var_dump(system('php -f t.php'));
?>
t.php is defined as following:
<?php
echo 'Hello world';
?>
Take a look at first comment in exec() manual.