please help. Problem external program through php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dubious
Forum Newbie
Posts: 2
Joined: Tue Mar 09, 2004 11:19 pm

please help. Problem external program through php

Post 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.
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
dubious
Forum Newbie
Posts: 2
Joined: Tue Mar 09, 2004 11:19 pm

Post 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 .
Post Reply