I have a simple php exec command that calls svnlook. If I run the command through the terminal I get all the output I expect. If I run it as shown below, I only get the last item.
$list = exec("svnlook changed -r ".$urlCleaned." ".$SVNEXPORT);
echo $list;
Can I buffer the output? If so how? And will that help?
That's by design and is explained:
string exec ( string $command [, array &$output [, int &$return_var ]] )
Return Values
The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.
To get the output of the executed command, be sure to set and use the output parameter.
http://php.net/manual/en/function.exec.php
exec("svnlook changed -r ".$urlCleaned." ".$SVNEXPORT, $output);
var_dump($output);
Alternatively, shell_exec returns everything.
Related
I wish to run external program with PHP and provide some arguments, like:
exec('C:\\Program Files\\iNFekt\\infekt\\infekt-cmd.exe -S --utf-16 '.$nfoFile, $output, $return_var);
But nothing happens, $output is empty array, $return_var is 1
What is my mistake here ?
Use shell_exec to get the output:
$output = shell_exec('C:\\Program Files\\iNFekt\\infekt\\infekt-cmd.exe -S --utf-16 '.$nfoFile');
From the Manual:
shell_exec — Execute command via shell and return the complete output as a string
I am unable to get php to show full results of my python script, only the last basic print statement is printed. What am I doing wrong here? Why is the php not showing the output from the python script?
php
$result = exec('python list.py');
echo $result;
list.py
import subprocess
print "start"
a = subprocess.Popen(["ls", "-a"], stdout=subprocess.PIPE)
a.wait()
result_str = a.stdout.read()
print result_str
print "stop"
the command line output for that python script is as below
start
...filename
...filename
...etc...etc
stop
The php output from executing the python script is only
stop
Thank you
You need to pass $result as a param into exec() otherwise you only get the last statement.
exec('python list.py', $result);
print_r($result);
This will get an array of output, $result[0] will contain stop and $result[sizeof($result)-1] will contain stop and everything in between will contain the rest of your program output.
exec will always return the last line from the result of the command. So, if you need to get every line of output from the command executed. Then, you need to write as below :
$result = exec('python list.py', $ouput);
print_r($output)
As per exec documentation
string exec ( string $command [, array &$output [, int &$return_var ]] )
If the output argument is present, then the specified array will be
filled with every line of output from the command. Trailing
whitespace, such as \n, is not included in this array. Note that if
the array already contains some elements, exec() will append to the
end of the array. If you do not want the function to append elements,
call unset() on the array before passing it to exec().
I am using a ffmpeg command for watermarking a video.
it does the work but i need to detect wheather it is executed successfully or not.
my command:
$mark = "ffmpeg -i ".$inputvideo." -i logo.png -filter_complex ". '"overlay=x=(main_w-overlay_w):y=(main_h-overlay_h)"'." ".uniqid()."html56.mp4";
For output i used something like:
$x = exec($mark);
print_r($x);
But i am not getting anything printed in place of $x.
After some searching I found this statement for exec command
string exec ( string $command [, array &$output [, int &$return_var ]] )
$s=exec($mark,$var);
$var is my return var.
Now when i print $var i am getting an empty array.
Please suggest where i am missing.
string exec ( string $command [, array &$output [, int &$return_var ]] )
When you do $s = exec($mark, $var); your $var corresponds to $output which is empty because ffmpeg outputs information to sderr since stdout might be used for actual data output.
If you want to get the return code all preceding optional arguments must be specified even if you don't use them:
$s = exec($mark, $output, $var)
If you need the actual output you can redirect the stderr to stdout since you're not using it by placing an 2>&1 at the end of your command or by using PHP's proc_open() to execute it.
Good Day everyone
Having a bit of a "Head Bang"on this. Im trying to find a way to search through files for a certain string. I know how to do it in linux by using a simple grep -c, but i'm unable to assign varaibles to the output. For example I want the file name as $var1 and the count as $var2, the reason for this is I will be using the data in a chart. I'm not to fixed on using linux commands, if there is any better way to do so I am open to using it. I would appreciate any info or direction of where to go and read up. My code:
<?php
$output = shell_exec ( "grep -c 'detected Tag 10055' *.txt" )
?>
This would the give me the file path and count as ../../public/130512.txt:100.
Thanks for any info
Use the another method in PHP to capture the output of the external program
string exec ( string $command [, array &$output [, int &$return_var ]] )
For example
$command = 'ls -ltr';
$response = exec ( $command, $output , $return );
print_r($output);
print_r($return);
The variable output array will contain your execution output, from that you can take the result.
The $response contains your last line of external program's output
$output contains the array of all output line by line
$return is the return value of your external program
I have an exec function in php file that execs a bash script. It script calls fmpeg to transcode a video file.
How can I know when transcoding is finish??
$script = "/opt/lamp../name.sh"
exec("$script $videoIn $id")
I will try using next code but it doesn't workd.
if (exec("$script $videoIn $id"))
{
//print on screen that the video has been transcoded
}
The function exec() will return when the executed command is finished. My guess is that the command fails somehow (possibly because you're not using escapeshellcmd() and escapeshellarg()).
Your php script waits for the exec'd command to be finished before going on.
exec does not return the command's return value.
string exec ( string $command [, array &$output [, int &$return_var ]] )
you have to provide a var where that value will be written.