Reputation: 3657
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
Upvotes: 2
Views: 10672
Reputation: 85458
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';
Upvotes: 6
Reputation: 127
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";
Upvotes: 1
Reputation: 28125
Depending on the security system* on the server, you will probably be able to do what you want.
However, considering you don't know much about PHP's syntax, I doubt you should be playing around with exec()
.
*such as suexec or chrooting apache, or enabling safe mode on PHP or running it as a separate service (on windows). Did you note a lot of things could go wrong?
Upvotes: 0
Reputation: 13259
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.
Upvotes: 1
Reputation: 3251
You can only execute the file, if it resides on the server machine and the server process has the proper rights to access/execute it. You cannot execute files on a remote users machine.
Upvotes: 0
Reputation: 47321
No, your example is a syntax error
Yes, if you quote the string :-
$path = 'C:\Programs\mydocs\file.exe';
Upvotes: 3