2

I am trying to execute c++ binary files, that i have generated using cmake and make command, from PHP. If i try to execute the same command from terminal everything works perfect but from php nothing seems to be happening. I checked the safe_mode, its off. I have given all executable permissions using chmod. I have tried putting stuff in same folder, different folder. In fact i have tried every possible solution i can think of but still i'm not able to execute these binary. Any idea why i'm not able to do so?

Any help would be really really appreciated.

Thanks in advance.

Procedure i used :-

if($dir !== FALSE) {
    $command = "./segmentation.sh $dir->dirname >> $dir->dirname/log"; 
    $output = $this->terminal($command);
    echo $output['output'];
}

terminal function

public function terminal($command)
{
    if(function_exists('system'))
    {
        ob_start();
        system($command , $return_var);
        $output = ob_get_contents();
        ob_end_clean();
    }
    else if(function_exists('passthru'))
    {
        ob_start();
        passthru($command , $return_var);
        $output = ob_get_contents();
        ob_end_clean();
    }
    else if(function_exists('exec'))
    {
        exec($command , $output , $return_var);
        $output = implode("n" , $output);
    }
    else if(function_exists('shell_exec'))
    {
        $output = shell_exec($command) ;
    }
    else
    {
        $output = 'Command execution not possible on this system';
        $return_var = 1;
    }
    return array('output' => $output , 'status' => $return_var);
}

And my shell script has a normal call to binary like

/path/to/folder/binary_file $args

I tried echo, ls, mkdir cmd from my shell script and it worked perfect only these binaries are not getting executed.

Varundroid
  • 9,135
  • 14
  • 63
  • 93

2 Answers2

0

It looks like your working directory may be different than you think it is.

Try this:

$path = realpath(dirname(__FILE__) );

$cmd = $path . $command;

and try executing that command (assuming the c++ file is in the same directory as the php file you are running.

Zak
  • 24,947
  • 11
  • 38
  • 68
0

It was MAMP that causing the issue.

If you are using MAMP like me then you have to perform one more thing.

Go to Menu->Go to Folder->Write "/Applications/MAMP/Library/bin/" in Text box->Press enter

Find file called "envvars" and comment the following lines using # :-

DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH" export DYLD_LIBRARY_PATH

Everything should work fine now.

Credit goes to this blog - Author Jonathon Hill

Varundroid
  • 9,135
  • 14
  • 63
  • 93