0

I can't get output from executable file (a.out) via php. my php code is :

$file = "/path/main.cpp";
$command="cd $path && c++ -lm ".$file;
exec($command);
$output = exec("cd $path && ./a.out");
exec("cd $path && ./a.out > res.txt");
var_dump($output);

all commands is executed but, I'm getting empty value;

exec("cd $path && ./a.out > res.txt");

this command also executed, res.txt was created in folder, but empty file. How to solve this problem? My OS is CentOS 7

5
  • Seems like you want popen. Commented Nov 18, 2019 at 8:36
  • Regarding the "empty file" from the redirection, are you sure that the program actually generates any output? Or that the compilation succeeded and the a.out program existing? Commented Nov 18, 2019 at 8:37
  • yes, I check from terminal, it's ok but via php no output Commented Nov 18, 2019 at 8:39
  • Try capturing the error output as well, see if it's failing (stackoverflow.com/questions/3863699/…). Commented Nov 18, 2019 at 8:41
  • I get this: sh: ./a.out: Permission denied Commented Nov 18, 2019 at 8:44

1 Answer 1

0

In order to get the output of a command with PHP, you can use the shell_exec function:

shell_exec

shell_exec — Execute command via shell and return the complete output as a string

So, try this:

$output = shell_exec("cd $path && ./a.out");
Sign up to request clarification or add additional context in comments.

4 Comments

sh: ./a.out: Permission denied
@BakhodirIsmatov so the problem is another... when you resolve it, shell_exec will do what you need
can apache run a.out ?
@BakhodirIsmatov try to run it from terminal if you can, otherwise try to run this in shell_exec: echo shell_exec('echo "it works";');. Because I don't know what does a.out contain, I can't help you on that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.