About exec() function in PHP

10,588

Solution 1

No, your example is a syntax error

Yes, if you quote the string :-

$path = 'C:\Programs\mydocs\file.exe';

Solution 2

Is it possible to run .exe file stored on my windows machine say on C: directory or the .exe file should be stored on the web server to run it through the php script.

PHP can only execute files on the server where it's running from (it also must have the right permissions to do so).

If you are trying to do what I think you are trying to do, which would be executing a command on the user's machine, what's to stop someone from using:

exec('format c:');

And format their visitors' C drives?


Can I store this path in a variable like

$path = C:\Programs\mydocs\file.exe;

and pass it like echo exec($path);

That will give you a syntax error, you'll need quotes:

$path = 'C:\Programs\mydocs\file.exe';

Solution 3

You can store the path like variable if you use the quote, in another case (with out quote) that will show you syntax error. Try with next two syntax:

$path = 'C:\Programs\mydocs\file.exe';

or double quote

$path = "C:\Programs\mydocs\file.exe";

Solution 4

Your question isn't entirely clear on where exactly your PHP script is running. If it's running on a web server that isn't your workstation, then quite simply no; you cannot open a local application through PHP running on a different server. The application that you wish to start up must be on the same server as the PHP script.

That said, if you wish to run an application on your local workstation or on the server, then it's simply:

exec('C:\path\to\program.exe');

Make sure to use either single-quotes ('C:\path\to\program.exe') or double backslashes with double quotes ("C:\path\to\program.exe"). Single-quotes are slightly more efficient and more appropriate in this case.

Share:
10,588
125369
Author by

125369

Updated on June 04, 2022

Comments

  • 125369
    125369 almost 2 years

    I need a small information regarding executing a .exe file using PHP script. I read that one can execute an .exe file using exec() or passthru() or echo system().

    Is it possible to run .exe file stored on my windows machine say on C: directory or the .exe file should be stored on the web server to run it through the php script.

    If so I need to provide the path for the .exe file to the exec() function, for example: file.exe is stored on C:\Programs\mydocs\file.exe.

    Can I store this path in a variable like

    $path =  C:\Programs\mydocs\file.exe;
    

    and pass it like echo exec($path);

    Loads of questions, I would like to know the views from the PHP profis.

    Thanks