13

I want php file to run exe file and display the exe file content when user goes to a particular url. I am trying to run exe file using php function exec('abc.exe');. But I only see blank page.

Anyone know how to solve it or how to run exe file from php file correctly? Many thanks in advance.

5 Answers 5

12

To access the operating system with php you do the following

$answer = shell_exec("abc.exe");
echo $answer."</br>"; 

The $answer string will contain the information that the abc.exe prints out or returns.

You may need to format it using explode().

1
  • Are you sure that your abc.exe is in the same directory as the path? When you run abc.exe from a terminal what do you see? Commented Sep 3, 2012 at 17:04
4

You can only run exe files if your php is runnning on a Windows machine. Futhermore, if you are on shared hostig, your hoster may have disabled the exec command.

If you are on a Windows machine, 'abc.exe' must be in the current directory or in the PATH.

To capture the output use:

exec( 'abc.exe', &$output);
echo $output;

Link to exec

3
  • Ya. It is window machine. I am on godaddy economical host. Is it allowed there?
    – ATZ
    Commented Sep 3, 2012 at 16:59
  • and what do you mean path? is it supposed to be like "abcd.com/abc.exe" ?
    – ATZ
    Commented Sep 3, 2012 at 17:03
  • As I say: abc.exe must be in the current directory, or in the PATH. Windows has to find the file! To be shure of your directory do echo getcwd();
    – JvdBerg
    Commented Sep 3, 2012 at 17:12
1

You can use VaccinalBowl code in windows, but for address .exe file, see the following example :

$answer = shell_exec("D://Downloads/software/npp.6.7.9.2.Installer.exe");
echo $answer."</br>";
1

I know this answer might be late but there is a "hack" in php you can use with Task Scheduler in Windows. The following code works and works with all browsers not just IE. However I should warn you that sometimes the program you are accessing may not run at that instant in time sometimes as I've noticed sometimes that the scheduled task may be in a "queued" state and not running. But the code does work 100%...it is just a matter of what state the task is in in task scheduler...either its "running" or "queued".

<?php
function ex($command)
{
    shell_exec('SCHTASKS /F /Create /TN _law /TR "' . $command . '"" /SC DAILY /RU 
INTERACTIVE');
    shell_exec('SCHTASKS /RUN /TN "_law');
    shell_exec('SCHTASKS /DELETE /TN "_law" /F');
}
ex("C:/Windows/System32/notepad.exe");
?>
2
  • I would also like to point out that commands containing UNC path or network path as you may know it also works....it is important to get the back slash format right as follows "\\\\ServerName\\Path\\to\\a\\someFile"
    – ymk
    Commented May 24, 2020 at 8:41
  • If you study the code well you will notice three steps. Firstly create a task "_law" to schedule notepad to open at that instant its created, secondly run the task (now notepad will open) and lastly delete the task after it opens. So if in the case you notice sometimes that notepad wont open and you want to know its state in task scheduler, you will have to comment out the delete task execution in the last shell_exec(). Then proceed to open task scheduler and check
    – ymk
    Commented May 24, 2020 at 9:27
0

Encountered with permission problem even with 2&>1 (although chmod 777), but workaround by reading the output via saving into a file

See the example, cs_crypto is something to decrypt

<?php
$str = $_GET['pswd'];
$output = shell_exec("echo $str");
echo "<pre><font color='white'>$output</font></pre>";
//$output = shell_exec("echo ./cs_crypto de aesbase $str");
//$output = shell_exec("./cs_crypto de aesbase $str 2>&1");
exec("./cs_crypto de aesbase $str > out");
$output = shell_exec("tail -1 out");
//exec('./cs_crypto de aesbase $str', $output, $return_var);
//echo "<pre><font color='white'>$return_var</font></pre>";
echo "<pre><font color='white'>$output</font></pre>";
?>

So the final result in web

enter image description here

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.