Executing .exe file using PHP on Linux server

2019-09-20 15:13发布

问题:

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!

回答1:

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



回答2:

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.



回答3:

  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.



标签: php linux shell