0

I am using shell_exec function of PHP to execute a binary .exe file of a C program.

My C program takes an input text file and in output creates several text files. To integrate it with web front end, I am using php's shell_exec function to execute a shell script which in turn calls the binary C .exe file.

This binary .exe file , input text file ,shell script and the php file is on the server side. I have given chmod 777 access to the mentioned files on server side for testing purposes.

The extract in php executing shell script is as follows:

echo shell_exec('sh segments.sh 2>&1');

Now, here I am executing a shell script and the shell script runs the binary .exe file as follows:

->segments.sh

./auto_import.exe input_file.txt -terms

auto_import is the C binary .exe file.

When I run the shell script individually in my terminal, it gives me proper output. That is the output will be 4-5 different text files.

But when I call the shell script with PHP, I am not able to get any output and I am not sure how to receive output from my binary file which is number of text files.

I have tried providing absolute paths to my files for eg:

In php file shell script which is called by:

echo shell_exec('sh /path/segments.sh 2>&1');

In shell script , binary file is called as follows

echo shell_exec('sh /path/binary 2>&1');

I have also used exec function as follows:

$cmd = "/auto_import.exe input_file.txt -terms"; echo exec($cmd,$output);

Could you gives help me out in this regard?

Vivek Ette
  • 60
  • 10

1 Answers1

0

If I understand you correctly, your command is executing as it should and producing files and you need to just read these now with PHP. One way of doing so is as follows:

<?php
$fileName = 'somefilethatyougenerated.txt';
$file = fopen($fileName, "r") or die("Unable to open file!");
$content = fread($file,filesize($fileName)); 
// manipulate contents here
fclose($myfile);
?>

If you need to read the file line by line, consider using "fgets", as shown here: How to read a file line by line in php

Community
  • 1
  • 1
Hayden Eastwood
  • 928
  • 2
  • 10
  • 20
  • Vivek your question needs to be clarified, right now it's a little ambiguous. If the shell_exec is not actually running your file it's most often an issue with the path. Make sure that when you specify shell_exec that you do so with the *absolute* path to the file to be executed so, for example, shell_exec("/usr/home/gary/php/binary.exe") rather than just shell_exec("binary.exe"). – Hayden Eastwood Jul 11 '16 at 21:21
  • I have used the absolute paths as well. But it did not work. – Vivek Ette Jul 11 '16 at 22:05
  • try running a different shell script from within PHP using the "system" command. For example, make a test.csh containing "echo 'test' " (or something to that effect) and test whether it gives any output using the "system" command: $testOutput = system('test.csh', $retval); see if $retval contains the output. LIkewise modify your script to give an output that you can test in the same way, using system once more. – Hayden Eastwood Jul 11 '16 at 22:58