How to know a code in php run sucessfully or not - php

I have the fallowing code
<html>
<body>
<?php
if ($_GET['run']) {
# This code will run if ?run=true is set.
echo "Hello";
exec ("chmod a+x ps.sh");
exec ("sh ps.sh");
}
?>
<!-- This link will add ?run=true to your URL, myfilename.php?run=true -->
Click Me!
Now i want to know exec ("chmod a+x ps.sh") is executing properly or not. What should i do??

Have a look at the documentation:
string exec ( string $command [, array &$output [, int &$return_var ]] )
...
return_var
If the return_var argument is present along with the output argument,
then the return status of the executed command will be written to this
variable.
So just check if the return code is not equals zero:
exec ("chmod a+x ps.sh", $output, $return);
if ($return != 0) {
// An error occured, fallback or whatever
...
}

exec(..., $output, $return);
if ($return != 0) {
// something went wrong
}
Capture the return code by supplying a variable name for the third parameter. If that variable contains 0 afterwards, all is good. If it's anything other than 0, something went wrong.

exec() accepts other parameters. The second one is output, it allows you to see the output of your command.
In the case of a chmod, a correct output is nothing.
The third argument of exec() is the return status. It should be 0 in case of success.
You should then do something like :
exec ("chmod a+x ps.sh", $out, $value);
if(!empty($out) || $value != 0)
{
#there was an error
}
Note : you don't have to initialize $out or $value beforehand, PHP will create them as you use them.

Related

Run a bash called from php and return a string

Hello I have the following PHP function:
public function index()
{
$return_var = exec('/home/iosef/createinstanceinfolder.sh');
print_r($return_var);
}
Which is calling the following bash script in ubuntu 20.04:
#!/bin/bash
$my_array=(foo bar)
$my_array[0]=foo
How can I return this array to PHP?
Exec takes 2 more parameters.
try:
$return_var = exec('/home/iosef/createinstanceinfolder.sh', $output, $retval);
Then print_r($output);
Also $output basically returns everything displayed by your script so in your script echo as a json or print_r your array in the end

How to capture return value from JAR in PHP and display it

I have following JAVA program which has been converted to JAR file and placed in the same directory as my PHP file.
So basically it takes an argument passed by PHP and displays it
public class Test {
public static void main(String[] args) throws Exception {
//Takes the value passed from the PHP
String Name = (new String(args[0])).toString();
//This will be treated as Output Parameter which will be returned to PHP
System.out.println("Return to PHP");
}
}
Below is my PHP code which will execute the JAR file and pass the required 1 parameter to the JAR.
<?php
$arg1 = "My_INPUT_PARAMETER";
shell_exec("java -jar TEST.jar $arg1");
echo "Done";
?>
I read somewhere that what ever the placed in Sysout (System.out.println) will be treated as output parameter or Return value to PHP.
So in my case it will be String "Return to PHP".
But I am not able to get the value to PHP and display it.
I tried placing a output value in the exec statement but its not working.
I tried below code but its throwing me error.
<?php
$arg1 = "My_INPUT_PARAMETER";
$output = '';
shell_exec("java -jar TEST.jar $arg1", $output);
echo "Done";
echo $output;
?>
Can anyone help me out here, How can I get a return value from PHP or output parameter from PHP and display it or use it in my PHP and continue with other execution part.
Thanks #Roland Starke.
So basically we can use 2 statements to run the JAR file from PHP:
EXEC and SHELL_EXEC.
EXEC will hold all the return values from JAR file and we can use it as Array and Display the required output parameter.
SHELL_EXEC will hold all the output parameters and it will display all at once.
<?php
$arg1 = "Multi Return";
exec("java -jar TEST.jar $arg1",$output);
echo $output[0]."<br/>";
echo $output[1];
echo "-------------------------------";
$shell_out = shell_exec("java -jar TEST.jar $arg1");
echo $shell_out;
?>

Php Exec function

I am unable to understand why the return value in php is not working. Can anybody help me?
<?php
exec("xyz.py",$output,$return);
foreach($output as $item){
echo "$item";
}
echo $return;
?>
The script of xyz.py is as follows:
def func():
print ('Hello')
return 21
func()
The output is always Hello0 no matter what value xyz.py returns
Thanks in advance.
According to the PHP docs, the third argument of exec, ($return in your example), works like this:
If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable
Since your python program ran fine, the return status should be 0 (no errors).
This might be what you want:
import sys
def func():
print ('Hello')
return 21
sys.exit(func())
Return parameter denotes the status of the exec() function and NOT what the program returns .
-1 = error
0 = success
Append this to your execution string, which allows you to capture any error output:
2>&1 &
So as per your example:
exec('xyz.py 2>&1 &', $output);
echo $output;

php exec() secure

I'm trying to execute a py file from php.
here is my code:
//usage python my.py var1 var2
$libre = 'python ../../../../root/py/my.py '.$var1.' '.$var2.'';
$cleanlibre = escapeshellarg($libre);
echo exec($cleanlibre);
What is wrong?
Why is it returning nothing?
I also need to know how to secure exec well. Thanks.
--edit--
Used passthru
$libre = 'python ../../../../root/py/mech.py '.$var1.' '.$var2.'';
$cleanlibre = escapeshellarg($libre);
passthru($cleanlibre, $result);
echo $result;
//returned 127 <- i don't know where thats from.
escapeshellarg shall only be used to escape the arguments, not the whole command.
//usage python my.py var1 var2
$libre = 'python ../../../../root/py/my.py '.escapeshellarg($var1).' '.escapeshellarg($var2).'';
echo exec($libre );
exec return result in second argument of function, see http://php.net/manual/en/function.exec.php

can't figure out an error in this one-liner. php + ffmpeg

if (!exec($ffmpegDo)) { $error[] = ERROR_EXEC_FFMPEGDO; }
You see, it's simple. If exec is executed, fine, else, return error.
The problem, the exec is getting executed here, but I also have the error.
Maybe you should read the function description. It doesn't return a Boolean. It returns the last line of the output.
What is $ffmpegDo and what does it output?
Maybe you should supply the &$return_var parameter. And check the return code. Ex.:
exec($ffmpegDo, $output, $return_var)
if ($return_var) { $error[] = ERROR_EXEC_FFMPEGDO; }
exec() returns the output of the command, not the return value. You probably want to do something like this instead:
exec($ffmpegDo, $output, $return_val);
if ($return_val) { $error[] = ERROR_EXEC_FFMPEGDO; }

Categories