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
Related
I've made a simple webpage that has to run some simple bash scripts. I run apache2 on Jetson Nano and I have three files in /var/www/html folder:
index.html
testexec.php
test.sh
First one looks like:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>TEST</title>
</head>
<body>
<form action="testexec.php">
<input type="submit" value="Create file">
</form>
</body>
</html>
the php file:
<?php
$output = shell_exec("test.sh");
header('Location: http://192.168.25.16/index.html?success=true');
?>
script:
#!/bin/bash
touch file.txt
My problem is that everything looks good, but the scrit doesn't run. In future it will be used to run program written in python, but for now I can't run even that simple one. Is the problem with location of files or with something else?
I've already tried to change php file
$output = shell_exec("test.sh");
with
$output = exec("test.sh");
with or without $output
My browser (firefox) returns no errors in console.
Script works fine when I run it from shell. It is executable.
I've already tried to look for similar problems, but there were no solutions.
Very often the problem is with user rights, first try installing chmod 777 test.sh to just check if this is the problem.
In addition, you should go back to the exec documentation
exec(string $command, array &$output = null, int &$result_code = null): string|false
and check what $result_code is equal to.
If we get echo result!:
$output=null;
$retval=null;
exec('echo $(pwd);', $output, $retval);
echo "${retval} with value:\n";
print_r($output);
My web host supports PHP. I have successfully run info.php and get pages of stuff to prove it in my browser.
When I try to execute a tiny piece of PHP in a tiny piece of HTML, as in the following hallowed script, I see nothing at all in my browser. Why? Is there something I am supposed to do?
Note: I use RapidWeaver as my principal site generation tool.
<html>
<head>
<title> PHP Test Script </title>
</head>
<body>
<?php
print "Hello World!";
?>
</body>
</html>
Make sure that your file extension is indeed .php, so that the web-server knows how to handle the file. Also, check your error-logs, there might be something in there if you get no output at all. You can do that in your terminal by running php --info | grep error. On your file-system, the error-log is stored in either /var/log/httpd/error_log or /var/log/apache2/error_log (if you're on Linux).
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 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>