How to run a file with PHP?

5

I wanted to know some code in PHP that makes me open a file .exe , ie a line that makes execute a .exe file from the same server.

    
asked by anonymous 02.12.2015 / 22:05

2 answers

9

As I had commented once the question was posted, you could use the exec() :

exec( 'caminho/do/executavel', [array &$retorno], [ int &$status_erro ] );

This function works well on both Linux and Windows, provided it is used with the correct paths, obviously.

It returns only the last line of the output. In a simple command like 'pwd', this function resolves without having to pass anything by reference. If you need a directory listing, for example, you already have to use the &$retorno parameter or for example the shell_exec() described below.

If you want to take the output of the executable directly to the screen or to download from the client side, you have passthru() (remember to set the correct headers in the application).

passthru( 'caminho/do/executavel', [ &$retorno ]);

The difference between the latter is that the data goes straight to the client, without you needing to give echo or any output function.

There is also system() , to execute commands as if they were executing directly on shell of Linux, or Windows CMD:

system ( 'caminho/do/executavel', [ &$retorno ] );

To stop the application while the executable runs, redirect the output to some stream or file (for example, caminho/do/executavel > /dev/null on Linux or > NIL on Windows)

Well remembered by @IvanFerrer, there is a "relative" of system() , which is shell_exec(); , which has a syntax difference that may help in some cases - the function return is the output of the command:

$listagem_do_diretório = shell_exec( 'ls -la' );

I will not go into detail, but it's good to comment that popen() and < a href="http://php.net/manual/en_US/function.pcntl-exec.php"> pcntl-exec() for some more specialized needs. More details can be seen in the manual.


Notes:

  • It is worth remembering that usually hosting server administrators disable these functions via PHP.ini, since a PHP invasion would compromise the rest of the server.

  • The & in the syntax examples above is only to indicate that the parameters are passed by reference. You should not type &$retorno in the actual code, only $retorno . Likewise, [ ] is also a syntax indication, and should not be understood literally. Do not put [ ] in real code.

03.12.2015 / 16:59
2

Use the PHP function exec () !

exec( 'caminho/do/executavel.exe', &$resultado);
echo $resultado;

Notes

  • exec must be enabled
  • 02.12.2015 / 22:11