Is there a way to run a shell script from PHP and echo the results after progress is completed?
Here is my shell script:
(Its multilines - a few commands that have to be ran one after the other. )
cd
cd /var/www/html/
npm uninstall gulp --save
npm install gulp --save
npm start
here's my currently functioning PHP script. It only outputs some of the progress and only outputs it when complete. I need a live preview of the progress.
<?php
echo "loaded<br><br>";
// echo shell_exec("cd /var/www/html/..");
// rin the shell scrip from header_register_callback
echo '<pre>';
// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('cd /var/www/html/; npm start', $retval);
// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>
Here is another approach. It uses redirection to output the results and then file_get_contents() to read the output.
<?php
echo "loaded<br><br>";
$cmd = 'npm start';
$run = $cmd . ' > result.txt 2> errors.txt';
$output = shell_exec($run);
$results = file_get_contents('result.txt');
if (strlen(file_get_contents('errors.txt')) != 0) {
$results .= file_get_contents('errors.txt');
}
echo "<pre>$results</pre>";
?>
I guess, ob_flush() would work:
<?php
echo "loaded<br><br>";
ob_flush();
// echo shell_exec("cd /var/www/html/..");
// rin the shell scrip from header_register_callback
echo '<pre>';
ob_flush();
// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('cd /var/www/html/; npm start', $retval);
// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>
But I don't understand why you echo HTML-tags when you run that script on the console... Hope I got you right
Related
I have a simple php code:
<?php echo exec('/opt/anaconda2/bin/python test2.py 2>&1'); ?><br>
And test2.py simply import a library called theano
import theano
It works under ssh but throw out:
KeyError: 'PATH'
when looking at the php in browser.
What's happeneing here? Is there any way that I can see full trace of error msg?
You can try this for the PHP side of things, but I think KeyError is a Python thing:
<?php
$output = array();
exec('/opt/anaconda2/bin/python test2.py 2>&1', $output, $returnCode);
echo 'Output is: ' . PHP_EOL;
var_dump($output);
echo 'Return code is: ' . PHP_EOL;
var_dump($returnCode);
?>
I'm trying to run a python script from a PHP file. for that I'm using system() function of PHP.
system('ls -l', $retval) is working as expected but system('python CMI.py', $retval) is not giving the desired results. The python script CMI.py is running perfectly fine when run from terminal.
I will give the code snippets and outputs so that there will be a clear picture of permissions.
<?php
echo '<pre>';
// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('ls -l', $retval);
// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
echo '<pre>';
// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('python CMI.py', $retval);
// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>
This is the output in the browser
How do I know there is error occured in ffmpeg command? and How do I get the error in my php?
below is my code
<?php
$cmd = "$ffmpeg -i $vid -an -ss $getfromsecond -s $size -vframes 1 $imageFile";
if(!shell_exec($cmd)){
echo "Thumbnail created" . "<br/>";
}else{
echo "Error Creating thumbnail". "<br/>";
}
?>
I am not sure if above approach is right.I have also tried below code
exec($cmd, $output, $return);
echo '$output :' ; print_r($output); echo "<br/>";echo '$return :' . $return . "<br/>";exit;
but in server it is just showing out put as
$output :Array ( )
$return127
I don't understand what is that error no, How to know if error has occurred and return the ffmpeg error no and ffmpeg error text , in php.
use it
exec($cmd,$test);
then check
if(file_exists($image))
{
echo 'suc';
}
I want to convert all pages of a PDF to JPEG. The PHP-script does what it should locally but fails on our live server.
$pdf = Zend_Pdf::load($file['tmp_name']);
echo 'Pages: '.sizeof($pdf->pages).'<br />';
for($pageCounter = 0; $pageCounter < $object->getNumberOfPages(); $pageCounter++) {
$pagenumber = $pageCounter + 1;
$currentfilename = $filename . "_$pagenumber.jpg";
echo $thumbPath.$currentfilename.'<br />';
echo $file['tmp_name']."[$pageCounter]<br />";
$return; $out;
exec("convert -density 250 '".$file['tmp_name']."[$pageCounter]' -quality 60 '$bigPath$currentfilename'", $out, $return);
var_dump($return); echo '<br />'; echo var_dump($out); echo '<br />';
exec("/usr/bin/convert -density 16 '/home/data/websites/www/meonline.be/public_html/images/magazines/".$file['name']."[$pageCounter]' -quality 60 '$thumbPath$currentfilename'", $out, $return);
var_dump($return); echo '<br />';
}
I tried to refer to the convert command by explicitly pointing to /usr/bin/convert & /usr/local/bin/convert. I always get 1 (or 127 if i'm pointing to the wrong folder) and the output array is always empty.
All folder as shown exist and have mode 777 set.
Here's a summary of all echoed output:
Full exec command:
convert -density 250 '/home/data/websites/www/meonline.be/public_html/images/magazines/HogeschoolPromotorenP-studenten.pdf[0]' -quality 60 '/home/data/websites/www/meonline.be/public_html/images/magazines/big/pLjB9_HogeschoolPromotorenP-studenten.pdf/hogeschoolpromotorenp-studenten_1399449783_1.jpg'
First echo echo 'Pages: '.sizeof($pdf->pages).'<br />';
pages: 1
Second echo echo $thumbPath.$currentfilename.'<br />';
/home/data/websites/www/meonline.be/public_html/images/magazines/thumb/pLjB9_HogeschoolPromotorenP-studenten.pdf/hogeschoolpromotorenp-studenten_1399449783_1.jpg
Third echo echo $file['tmp_name']."[$pageCounter]<br />";
/home/data/websites/www/meonline.be/public_html/images/magazines/HogeschoolPromotorenP-studenten.pdf[0]
Fourth echo var_dump($return); echo '<br />'; echo var_dump($out); echo '<br />';
int(1)
array(0) { }
UPDATE
Someone executed my command in the terminal and got the following response:
convert: Postscript delegate failed `/home/data/websites/www/meonline.be/public_html/images/magazines/HogeschoolPromotorenP-studenten.pdf': No such file or directory # error/pdf.c/ReadPDFImage/713.
convert: no images defined `/home/data/websites/www/meonline.be/public_html/images/magazines/big/fs5Jd_HogeschoolPromotorenP-studenten.pdf/hogeschoolpromotorenp-studenten_1399455323_1.jpg' # error/convert.c/ConvertImageCommand/3150.
I'm 100% sure that the PDF file exists and the second line complains about the image not being defined, but this command should create them for me.
In case anyone is looking for an answer, the problem was actually quite simpel.
The local server had Ghostscript installed, the live server didn't...
Download it here and follow the instructions here.
In my case, I didn't have access to the commandline on the live server. In that case, check if /usr/bin/gs is available (in which case it is installed) with this command from PHP:
$return; $out;
exec("/usr/bin/gs 2>&1", $out, $return);
var_dump($return); echo '<br />'; echo var_dump($out); echo '<br />';
I need to read the output from ffmpeg in order to even try the solution to my question from yesterday. This is a separate issue from my problem there, so I made a new question.
How the heck do I get the output from an ffmpeg -i command in PHP?
This is what I've been trying:
<?PHP
error_reporting(E_ALL);
$src = "/var/videos/video1.wmv";
$command = "/usr/bin/ffmpeg -i " . $src;
echo "<B>",$command,"</B><br/>";
$command = escapeshellcmd($command);
echo "backtick:<br/><pre>";
`$command`;
echo "</pre><br/>system:<br/><pre>";
echo system($command);
echo "</pre><br/>shell_exec:<br/><pre>";
echo shell_exec($command);
echo "</pre><br/>passthru:<br/><pre>";
passthru($command);
echo "</pre><br/>exec:<br/><pre>";
$output = array();
exec($command,$output,$status);
foreach($output AS $o)
{
echo $o , "<br/>";
}
echo "</pre><br/>popen:<br/><pre>";
$handle = popen($command,'r');
echo fread($handle,1048576);
pclose($handle);
echo "</pre><br/>";
?>
This is my output:
<B>/usr/bin/ffmpeg -i /var/videos/video1.wmv</B><br/>
backtick:<br/>
<pre></pre><br/>
system:<br/>
<pre></pre><br/>
shell_exec:<br/>
<pre></pre><br/>
passthru:<br/>
<pre></pre><br/>
exec:<br/>
<pre></pre><br/>
popen:<br/>
<pre></pre><br/>
I don't get it. safe_mode is off. There's nothing in disable_functions. The directory is owned by www-data (the apache user on my Ubuntu system). I get a valid status back from exec() and system() and running the same command from the command line give me tons of output. I feel like I must be missing something obvious but I have no idea what it is.
The problem is you catch only stdout and not stderr (see Standard Streams).
Change this line:
$command = "/usr/bin/ffmpeg -i " . $src;
into
$command = "/usr/bin/ffmpeg -i " . $src . " 2>&1";
and give it another try :)
Use ffprobe instead, it's much quicker and supports JSON output.
$output = shell_exec('ffprobe -v quiet -print_format json -show_format -show_streams "path/to/yourfile.ext"');
$parsed = json_decode($output, true);
And you have all your video info in a php array! This is much faster than ffmpeg -i for some reason.
To get output status and output:
exec("ffmpeg -i input.avi output.mp4 2>&1", $output, $returnStatus);
print_r($output);
if($returnStatus === 0){
// success
}
else {
//fail
}
You can use exec and print_r the output...
exec("ffmpeg -i input.avi -vcodec h264 -acodec aac -strict -2 output.mp4 2>&1",$output);
echo "<pre>";
print_r($output);
echo "</pre>";