1

Here is the problem: I have 5 bin files generated by c++ code, I'm developing a GUI for execute that files using php pages. I try to execute the bin file with the php function exec. When I load the page nothing appends, I want that the bin starts running local showing me the results in the shell window. calibrate is the name of the bin file.Is it possible?

 <html>
  <body>
  <?php 
   exec('calibrate');
  ?><br>
  </body>
 </html>  

UPDATE

I checked the PHP error log and I have a permission denied error, the problem is that I modified all the permissions with 777 (for the moment) also in the parent directory but I receive the same error. I don't know if can be useful but if I call the command whoamII read apacheI. Hoping someone can help.

user3050386
  • 139
  • 1
  • 9
  • 1
    Try giving `exec()` the full path of the script. – John Bupit Aug 24 '15 at 10:59
  • Try enabling error reporting during development so you see what goes wrong on the page. Or look in the php error log. (Sometimes found in the webserver log) – BubuIIC Aug 24 '15 at 11:05
  • You want to run php script from shell that will exec bin file and output results? Why would you include php tags withing html tags though? – Jan.J Aug 24 '15 at 11:05

3 Answers3

0
  1. Type full path of bin file in the exec function

    exec("C:\xampp\apache\bin\ax.exe");
    

    2.Also confirm exec function is not listed in the disabled functions list in php.ini

ManiMuthuPandi
  • 1,594
  • 2
  • 26
  • 46
0

From the official documentation for exec(), it returns the last line from the result of the command:

Return Values

The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.

To get the output of the executed command, be sure to set and use the output parameter.

If you have only one line of output, then simple echo it:

<?php echo exec('/path/to/calibrate') ?>

Otherwise, pass in the second argument to get the array of output:

<?php
   $output = array();
   exec('/path/to/calibrate', $output);

   // Print it however you need
   print_r($output);
?>

If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().

John Bupit
  • 10,406
  • 8
  • 39
  • 75
  • I think that the path is right, but I don't want print the output in the webpage, I want only have the same results that I have if I write calibrate in the shell. It starts the calibration of my hardware and show 3 pages of results – user3050386 Aug 24 '15 at 12:26
0

This worked for me after searching for hours! Good Luck! :)

semanage fcontext -a -t httpd_sys_script_exec_t '/whatever/scripts(/.*)?' 
restorecon -R -v /whatever/scripts/

Tell SELinux to Give Apache Execute Access to PHP Files Outside Document Root

Prashant Pokhriyal
  • 3,727
  • 4
  • 28
  • 40
Trent
  • 66
  • 4