PHP exec not executing python script with module imports - raspberry-pi

OK so im trying to get exec() to execute a script that works fine from idle.
If I try a simple script it works fine, such as:
hello = "hello"
world = "world"
print hello + " " + world
But the script I actually want to run wont work and I can only assume its because of the module import, the code is as follows:
import serial
port = serial.Serial('/dev/ttyAMA0', baudrate=19200, bytesize=8, parity=serial.PARITY_NONE, stopbits=1, timeout=5)
port.open
port.write("\x02\x00\x00\x00\x00\x02")
My PHP code is:
<?php
$result = exec('python python.py');#This works fine
echo $result
?>
<?php
$result1 = exec('python proOn.py');# This wont work
echo $result1
?>

Try this:
<?php
$command = escapeshellcmd('proOn.py');
$result1 = shell_exec($command);
echo $result1;
?>
refer this.

Related

How to add PATH in shell script

SRC_FILE=$1
BKP_FILE=$2
echo "SRC_FILE :" ${SRC_FILE}
echo "BKP_FILE :" ${BKP_FILE}
In the above code how can I give the direct path to SRC_FILE and BKP_FILE. like if I want to give the path say, "app/retro/power/shell" to SRC_FILE and "app/retro/power/script" to BKP_FILE.
How do I fit the above path to the script??
You should just pass the argument when calling your script.
SRC_FILE=$1 BKP_FILE=$2
echo "SRC_FILE :" ${SRC_FILE}
echo "BKP_FILE :" ${BKP_FILE}
So the PATH for SRC_FILE and BKP_FILE should be passed like this :
./script.sh SRC_FILE BKP_FILE
EDIT:
If you want to write down the PATH directly into the code you would set the variables as follows:
SRC_FILE="app/retro/power/shell" BKP_FILE="app/retro/power/script"
echo "SRC_FILE :" ${SRC_FILE}
echo "BKP_FILE :" ${BKP_FILE}

Why preg_replace() function isn't working properly?

My PHP Script is:
<?php
$string = '{controller}/{action}';
$pattern = '/\{([a-z]+)\}/i';
$replacement = '(?P<$1>[a-z-]+)';
echo preg_replace($pattern, $replacement, $string);
?>
it is showing this result:
(?P[a-z-]+)\/(?P[a-z-]+)
I am expecting this:
(?P<controller>[a-z-]+)\/(?P<action>[a-z-]+)
How I can able to do this??
Your code produces the correct result, that is,
(?P<controller>[a-z-]+)\/(?P<action>[a-z-]+)
The problem is: when you echo that out and display it in a browser, the browser interprets <controller> and <action> as HTML tags, like <p> or <strong>. So, it doesn't display them; it only displays what is left:
(?P[a-z-]+)\/(?P[a-z-]+)
You would see the correct result if you ran this script from the command line. To make it work in the browser, you need to replace the last line with
echo htmlentities(preg_replace($pattern, $replacement, $string));

ZF2 How I can do a session echo in my view?

Do you how I can do to print a session variable in my view as:
<?php echo $this->session->user_id; ?>
Thanks you very much !
Edit:
To call a session variable in your view, you have to use this code:
$session = new \Zend\Session\Container('session_key');
echo $session->user_id;
try
$session = new \Zend\Session\Container('session_key');
echo $session->user_id;

PostgreSQL query works but not with php

I want to make a php query to a PostgreSQL database. I tested the query in the server and returns, but when I try it in php:
<?php
//Check the input
if (!isset($_POST['variable']))
echo "Address not selected";
$input = $_POST['variable'];
//$input = ucfirst(trim($_POST['variable']));
//echo $input;
$conn = pg_connect("host=localhost port=5432 dbname=geocoder user=postgres");
if (!$conn)
echo "Could not connect to server..";
$sql = "SELECT (addy).stateabbrev FROM geocode($input);";
$result = pg_query($conn, $sql);
if (!$result)
echo "Query did not executed..";
?>
I get that "Query did not executed..";
The string for the QUERY is taken from a html page using javascript.
In the error.log of Apache2 i get:
PHP Warning: pg_query(): Query failed: ERROR: syntax error at or near "Penn"\nLINE 1: SELECT (addy).stateabbrev FROM geocode(O
What can be the point here?
Sanitize the user supplied data:
$input = pg_escape_literal($conn, $input);
$sql = "SELECT (addy).stateabbrev FROM geocode($input);";
I managed to find the problem, which is the fact that $input variable from geocode() function, must be surrounded by single quotes:
geocode('$input')
:)

PHP - echo inside an echo

I have a PHP if/else statement. This is the code I'm trying to echo under an else condition.
<?php $locked = ForumData::is_topic_locked($post->topic_id);
if ($locked->topic_locked == 1) {echo '<td align="right"><font color="#FF0000">Topic Locked</font><td>';}
else {
echo '<td align="left"><img src="<?php echo SITE_URL?>/lib/skins/flyeuro/images/forums/t_reply.gif"/></td>'; }
?>
The bit I'm interested to echo is this.
<img src="<?php echo SITE_URL?>
If I try this... 'echo SITE_URL'
Parse error: syntax error, unexpected T_ECHO, expecting ',' or ';'
But this doesn't parse the image, and if I try parsing anything else, it's giving me parsing errors, which I can't fix?
How can I therefore produce an echo inside another echo?
why did you open a <?php tag again, you are already in echo line?
echo '<td align="left"><img src="'.SITE_URL.'/lib/skins/flyeuro/images/forums/t_reply.gif"/></td>';
and what is SITE_URL? Is that a variable, did you forget to put $?
echo prints out the string that you gave as parameter,
echo "foo";
As #hakre mentioned about it, . is used to concatenate strings.
$var = "foo"."bar"; //foobar
So you can use it in echo line,
$var = "foo"."bar"; //foobar
echo "foo "."bar ".$var // foo bar foobar
And It's not important weather variable defined as a string. It would be a constant variable.
define('SITE_URL', 'localhost:8080/phpvms');
echo "my website URL is ".SITE_URL; //my website URL is localhost:8080/phpvms
Remember:
<?php echo "View"; ?>
" and \
this!
Hope that's enough of a hint!#
Your problem is probably solved this way:
echo '<td align="left"><a href="',
url('Forum/create_new_post?topic_id=' . $post->topic_id . '&forum_id=' . $post->forum_id . '') ,
'"><img src="', SITE_URL,
#######################
'/lib/skins/flyeuro/images/forums/t_reply.gif"/></a></td>';
In PHP you can use constants quite like variables, e.g. to output them. You don't need to stack echoes inside each other or something.