Calling C++ program from PHP on linux

I want to call a C++ program from PHP with apache server on Fedora 12 linux.
I see the output of exec(‘ls -lrt’) but not exec(“testprog”).
testprog is put in the same directory of the test php program.

Please help.
The following are my php and cpp test programs.

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
<?php
echo exec('ls -lrt');
echo exec("testprog");
?>
 </body>
</html>

testprog.cpp

#include <iostream>
#include <algorithm>

int main() {
  int array[] = { 23, 5, -10, 0, 0, 321, 1, 2, 99, 30 };
  int elements = sizeof(array) / sizeof(array[0]); 
  std::sort(array, array + elements);
  for (int i = 0; i < elements; ++i) 
     std::cout << array[i] << ' ';
}

Try exec(“./testprog”)

It works!
Thanks.
Why setting the currrent path “.” by the environment variable PATH does not work?
echo $PATH
/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/games:/usr/local/sbin:/usr/sbin:/sbin:.

The reason is that the “exec” shell is not your login shell and doesn’t respect your .profile .

In general it’s better to use absolute paths rather than relying on the current dir, which is not necessary the script directory.


$path = dirname(__FIILE__) . "/testprog";
exec($path);