1

I'm new in using Linux, I'm trying to write a PHP code which can run .exe linux compatible file, I've made a short shell script

hello bash script:

#!/bin/bash

./program.exe file.mp4   // file.mp4 is an an input for .exe 
echo "Hello World!"

shell.php:

<?php

$output = exec ("./hello ");
echo "<pre>$output</pre>";
?>

Now when I run shell.php using web browser it shows Hello World! but the .exe doesn't run, however when I run php using terminal command php shell.php, It works fine.

I think I'm having problems with permissions but I'm new with Linux and I don't know how to solve this.

Update:

I ignored the shell script and I used

<?php

$output = shell_exec ("cd /var/www/ && ./program.exe file.mp4 2>& " );

?>

also I granted access to program.exe

chmod 777 program.exe 

the error I receive in the browser :could not open debug.bin!

user2140147
  • 31
  • 1
  • 3
  • use the absolute path to hello executable exec("sh path/to/the/file") – rams0610 Sep 10 '13 at 08:40
  • 2
    you need wine to execute .exe files under linux and its not garantied to work at all. How did your exe file even get to your linux ? If you know what your doing: your missing the ; after exec(); and after echo "
    ";
    – Christoph Diegelmann Sep 10 '13 at 08:44
  • 2
    @Christoph: one can name linux executables .exe. but every time someone does it, a cute little kitten dies. – ciruvan Sep 10 '13 at 09:11
  • https://stackoverflow.com/a/53840191/308851 related, somewhat. – chx Dec 18 '18 at 20:00

3 Answers3

1

use the absolute path to hello executable exec("sh path/to/the/file")

rams0610
  • 1,041
  • 10
  • 8
  • I think I already can access the shell file "hello", because It prints the echo message "Hello World!" when I call it, but It doesn't run .exe file. @Christoph I create .exe file from a C++ code using Make command in terminal, then I use this .exe file. also sorry for forgetting ";" but I didn't copy the code I typed it because It's on another machine. – user2140147 Sep 10 '13 at 08:55
1

I'm using something similar to call an app compiled with mono on a remote ubuntu webserver and return it's output to the calling script.

For any of this to work properly wine needs to be already installed. On Ubuntu systems try:

sudo apt-get -y install wine

You then need to know the owner of the web server process. If you are running the apache web server try the following:

cat /etc/apache2/envvars | grep "RUN"

The output will look something like this:

export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
export APACHE_RUN_DIR=/var/run/apache2$SUFFIX

Now that you have the name of the process owner, which in this case is www-data you should ensure the file is owned the user and its group:

sudo chown www-data /var/www/program.exe
sudo chgrp www-data /var/www/program.exe

Finally, we can invoke the application from inside our PHP script by passsing it as a parameter to 'wine' and using its full file path.

<?php
    $output = shell_exec("wine /var/www/program.exe file.mp4" );
?>

Any output from the above shell command sent to the command line will be saved in the PHP script variable $output.

It looks like you are trying to do some output redirection with your use of program.exe file.mp4 2>& so I've left that off of the example for clairity.

Andre F
  • 423
  • 4
  • 11
0
  1. Try using the absolute path, such as exec("sh /path/to/file")

  2. Generally, php is run as www or apache, so make sure that the execute access permission is granted to all user.

Leo Zhuang
  • 479
  • 2
  • 9
  • On Ubuntu Apache runs as www-data. – Charlie Vieillard Sep 10 '13 at 08:51
  • Regarding 2 above, it's generally better to grant `+x` to the web server user unless you really want everyone to be able to execute it. In some cases the web server is actually equivalent with "everyone". – Qben Sep 10 '13 at 08:51
  • Thank you for the help, I modified PHP to be $output = shell_exec("cd /var/www/ && ./program.exe input.mp4 2>&1"); I also granted access using chmod 777 program.exe , I get an error in browser "Could not open debug.bin!, Sorry If the question is basic but I'm new with Linux – user2140147 Sep 10 '13 at 10:37