Vivek Ette
Vivek Ette

Reputation: 60

using php shell_exec to execute a binary file to capture output files

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?

Upvotes: 0

Views: 1870

Answers (1)

Hayden Eastwood
Hayden Eastwood

Reputation: 966

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

Upvotes: 0

Related Questions