Page 1 of 1

please help. Problem external program through php

Posted: Tue Mar 09, 2004 11:19 pm
by dubious
I am trying to write a php script which will run another program that I have in the same folder. This other program returns some output to the screen and I want to be able to use the output, maybe write to a file or show it on the screen. The problem that I am facing is that the code works fine if I am logged in and on the command prompt type php scriptname.php but If I run the same script from the web browser I don't see any output. I have checked the apache logs, no error. I have set all permission to full on files that are being used.

The following is the code.

<?php
$pathN = "/home/httpd/html/testuser";
//$comm = "./FeatureExtraction $pathN/uploaded/db_image_query.ppm > $pathN/shape/testfile";
$comm = "./FeatureExtraction $pathN/uploaded/db_image_query.ppm";
echo $comm."<BR>";
$result=exec($comm,$output);
echo $output;
$comma_separated = split(" ", $output);
if(strlen($output)>1)
{
echo "<BR>yes";
echo $comma_separated[1];
echo "$output\n";
foreach ($output as $p)
{
echo $p." ";
}
}
else
{
echo "<BR>no";
echo "<BR>result=$result";
}
echo "\n";

?>


I would appreciate any responses, because I have spent alot of time on this problem and I have a feeling that the problem could be very trivial.


Thanks.

Posted: Wed Mar 10, 2004 5:57 am
by AVATAr
From the manual

exec() executes the given command, however it does not output anything. It simply returns 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.

Posted: Wed Mar 10, 2004 9:52 am
by pickle
A function I have found more useful than passthru() is shell_exec(). It works just like passthru() except you can assign the output of the command to a string, and deal with it as you see fit.

Posted: Thu Mar 11, 2004 1:05 am
by dubious
Thanks alot guys, I finally fixed it. I used shell_exec but I also had to do sudo with root permission.
I also used 2>&1 .