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!

4
  • use the absolute path to hello executable exec("sh path/to/the/file") Commented Sep 10, 2013 at 8: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 "<pre></pre>"; Commented Sep 10, 2013 at 8:44
  • 2
    @Christoph: one can name linux executables .exe. but every time someone does it, a cute little kitten dies. Commented Sep 10, 2013 at 9:11
  • stackoverflow.com/a/53840191/308851 related, somewhat. Commented Dec 18, 2018 at 20:00

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

Comments

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.

3 Comments

On Ubuntu Apache runs as www-data.
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".
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

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.