So is there a way to open local file in default program from local browser app?
Tried using simple html a href
file:///filepath/filename.exe
doesn't work due to security policies, says:
Not allowed to load local resource
Tried using php exec
exec('notepad');
Seems like it opens it server-side but not client-side.
Also tried
exec('runas /admin notepad')
Says
Enter the password for admin:
I know there lots of duplicates and I spent some time researching, but cannot find decisive answer is it possible? and if it is, how?
Not possible.
PHP is a server side language, no PHP is seen by the client browser.
Furthermore, this can not even be done using a client side browser language such as javascript; reason being, this would be a huge security vulnerability in that it would allow any website you visit to arbitrarily execute / launch applications on your local machine.
PHP can't be used to start a program client side. Also, javascript is not capable of doing this either: How can I run a program or batch file on the client side? as it would be a security risk.
However there is one way to do it but this can't be used to open every type of file. For example, sending a user the address mailto:example#email.com will prompt the user to open the default program for email. If your program supports this, then you can open it that way. This might not be possible in your use case, but you can make your own protocol like this if you can install something on their computer: https://msdn.microsoft.com/en-us/library/aa767914(VS.85).aspx
Hooray, It worked!
Thanks to this guy.
It IS POSSIBLE to run any external GUI program with php with 3 lines of code:
shell_exec('SCHTASKS /F /Create /TN _notepad /TR "notepad.exe" /SC DAILY /RU INTERACTIVE');
shell_exec('SCHTASKS /RUN /TN "_notepad"');
shell_exec('SCHTASKS /DELETE /TN "_notepad" /F');
Basically you are scheduling a task, then running, then deleting it.
Related
I'm running in a controlled, xp, intranet only environment and I need to start external processes from a PHP applications. (Backups, Reports etc.)
I can get system or exec to start processes that work silently. Here's a simple example
<?php exec ("echo hello > hello.txt");?>
I can get it to execute a bat file that has no visible output.
I can't get any program that has a screen to run such as a report generator or notepad...
<?php exec ("explorer");?>
doesn't do anything. or same for system
Very late answer, but I was working on this myself and found that it is indeed possible to run a GUI program from PHP with the Apache server on Windows XP.
Start->Run, type "services.msc" to bring up Services control (other ways to get there, this is easiest IMO)
Locate your Apache service (mine was called "wampapache" using WampServer 2.0)
Open the service properties (double-click or right click->properties)
Flip to the Log On account and ensure the checkbox titled "Allow service to interact with Desktop" is checked
Flip back to the General tab, stop the service, start the service
Now, using the code below, you can spawn UI processes from PHP. In the first code snippet, the script will not wait for the application to close; the second snippet waits for the program to close before continuing (blocking).
Do not wait for application:
pclose(popen("start /B notepad.exe", "r"));
Wait for application:
system('start notepad.exe');
This has been tested on Windows XP. I have not tried it on any other Windows versions, your millage may vary.
Side note
On my particular installation, I was using the other option in the Log In tab of the service - Apache was running as a domain user so it could access several network shares with domain user permissions. The checkbox isn't available for that option, only when the service is running as Local System. After extensive research, I've found that there is simply no way for a single service to both interact with the current desktop AND utilize the credentials of a specific user. It's a one-or-the-other proposition, with the suggested remedy being to split your service into two components - one that uses the user account privs and one that interacts with the desktop. Not very practical when the service you're talking about is the web server. This note is probably pretty specific to my use case, but I wanted to put it out here in case I can save someone else the frustration in the future.
Another super late answer, but this comes up on Google when searching for "php run gui program"...
I have been able to launch a GUI app in Windows 8.1 by making, running and deleting a scheduled task:
shell_exec('SCHTASKS /F /Create /TN _notepad /TR "notepad.exe" /SC DAILY /RU INTERACTIVE');
shell_exec('SCHTASKS /RUN /TN "_notepad"');
shell_exec('SCHTASKS /DELETE /TN "_notepad" /F');
What behavior are you expecting? Calling system('notepad') works fine - it just doesn't display the GUI. It runs in the background, and PHP sits there patiently waiting for notepad to close itself (and only continues if you kill notepad from the process list).
If you're expecting it to pop up a GUI, I'm fairly certain that you can't do that. ;) An option might be to write out a batch script (file_put_contents('runme.bat', 'notepad hello.txt')) and have that batch script queued (with Windows scheduler or whatever the cron-equivalent is on Windows) to run in an async fashion (and clear itself at the end).
I have a script written in PHP. my PHP server is running on a Windows Server.
How can I schedule a task that will open a web link "URL" and after the page finish executing close the browser.
Thanks
The answer of Julian Knight has some drawbacks:
It will leave IEInstances open, which you have to kill manually... While searching for a "One-Liner" to achieve the same goal, I found powershell to be very helpful:
Task-Scheduler, target: Powershell.exe, use arguments:
-Command "(New-Object Net.WebClient).DownloadString('http://myhost/cron/cron.php')"
This will download the whole content (render the whole DOM), and then quit the powershell-instance.
As others have said, you use the Windows Task Scheduler which allows similar functionality to CRON but also a lot more (startup/down, events, etc.)
You can call Internet Explorer from a command with the url as a parameter:
iexplore.exe "http://etc.etc.etc"
You may need to explicitly add the path to iexplore.exe
Of course, as you say, you would then need to kill the browser process afterwards.
Better would be to add a simpler command to a folder on your path such as CURL for Windows or WGET for Windows. Direct the output of the command to null, the command will exit with a testable return code should you need to further check whether it worked.
A third option would be to use PowerShell though I think that would be overkill in this case. I'd use it if I needed to test the return code for logging or to execute some other task on failure (e.g. write an error to the Windows Event Log and have a second task set up to run when that event occurs).
A 4th option would be to call PHP as this is supported if you have PHP installed on the server. Again though, the overheads of startup wouldn't make this worthwhile.
Personally, I would use WGET.
I have a windows pc with apache running, and I needed a php script to continuously run to listen to inputs coming from a UDP port, and take the required action and send it back.
The only way I know how to do this, is to install curl for cmd, and run the php script with a WHILE loop. What I am afraid is that this is the wrong way to do it.and may be unreliable and take up large amount of system resources.
Can people comment on the above method? I have heard of cron..but thats for unix only? What can I do?
Hey try this below solution.
Use a bat file and schedule to execute that bat file.
For example in the bat file executephp.bat, write this
c:\xampp\php\php.exe -f c:\xampp\htdocs\do_something.php
save that bat file that contains that line.
Go to windows scheduler and create a new task and in action tab, browse to point that executephp.bat and for start in -> direct to the directory u have that executephp.bat.
For example if u save the file under C:\xampp\htdocs put that C:\xampp\htdocs in the start in.
Remember to invoke the script even when the user is not logged on.
Everything is set and it will execute without problem.
A PHP script behind Apache will always have a maximum execution time, so the while-loop should always be stopped after the specific timeout.
You should better use cron or a batch script like Venkat recommended. There are some great services for cron out there, that will do a GET request to your server and run the script. Have a look at this related thread: Scheduled Request to my website from an external source
Doesn't that fit your needs?
Is it possible to invoke php script automatically when someone uploads file via ftp-client on our server.
Do you have complete shell access to the server?
What you need to do is detect whether or not the contents of a folder has changed and have a script run.
If you're on Windows this might be helpful.
If you're on *nix, have a look into inotify or launchd
Have them call a php script and away you go!
If you don't have complete control over the server, no doubt you can execute Cron Jobs. Have it execute a PHP script every x minutes that checks the contents of a directory, compares it to the contents it was x minutes ago and performs a diff between the two to find added or removed files.
You can implement a port knocking daemon with iptables. Port knocking is an automate process to personalize ssh or ftp account. You can write a daemon to listen to iptables and run a php script when the right sequence of ports is knocked. I don't know if you can minimize the sequence to just one knock when you connect with a ftp-client.
At linux you can use the watch command:
watch
Usage: watch [-bdhnptvx] [--beep] [--differences[=cumulative]] [--exec] [--help] [--interval=<n>] [--no-title] [--version] <command>
You can pipe the output to a piece of software actually handling the changes.
I'm running in a controlled, xp, intranet only environment and I need to start external processes from a PHP applications. (Backups, Reports etc.)
I can get system or exec to start processes that work silently. Here's a simple example
<?php exec ("echo hello > hello.txt");?>
I can get it to execute a bat file that has no visible output.
I can't get any program that has a screen to run such as a report generator or notepad...
<?php exec ("explorer");?>
doesn't do anything. or same for system
Very late answer, but I was working on this myself and found that it is indeed possible to run a GUI program from PHP with the Apache server on Windows XP.
Start->Run, type "services.msc" to bring up Services control (other ways to get there, this is easiest IMO)
Locate your Apache service (mine was called "wampapache" using WampServer 2.0)
Open the service properties (double-click or right click->properties)
Flip to the Log On account and ensure the checkbox titled "Allow service to interact with Desktop" is checked
Flip back to the General tab, stop the service, start the service
Now, using the code below, you can spawn UI processes from PHP. In the first code snippet, the script will not wait for the application to close; the second snippet waits for the program to close before continuing (blocking).
Do not wait for application:
pclose(popen("start /B notepad.exe", "r"));
Wait for application:
system('start notepad.exe');
This has been tested on Windows XP. I have not tried it on any other Windows versions, your millage may vary.
Side note
On my particular installation, I was using the other option in the Log In tab of the service - Apache was running as a domain user so it could access several network shares with domain user permissions. The checkbox isn't available for that option, only when the service is running as Local System. After extensive research, I've found that there is simply no way for a single service to both interact with the current desktop AND utilize the credentials of a specific user. It's a one-or-the-other proposition, with the suggested remedy being to split your service into two components - one that uses the user account privs and one that interacts with the desktop. Not very practical when the service you're talking about is the web server. This note is probably pretty specific to my use case, but I wanted to put it out here in case I can save someone else the frustration in the future.
Another super late answer, but this comes up on Google when searching for "php run gui program"...
I have been able to launch a GUI app in Windows 8.1 by making, running and deleting a scheduled task:
shell_exec('SCHTASKS /F /Create /TN _notepad /TR "notepad.exe" /SC DAILY /RU INTERACTIVE');
shell_exec('SCHTASKS /RUN /TN "_notepad"');
shell_exec('SCHTASKS /DELETE /TN "_notepad" /F');
What behavior are you expecting? Calling system('notepad') works fine - it just doesn't display the GUI. It runs in the background, and PHP sits there patiently waiting for notepad to close itself (and only continues if you kill notepad from the process list).
If you're expecting it to pop up a GUI, I'm fairly certain that you can't do that. ;) An option might be to write out a batch script (file_put_contents('runme.bat', 'notepad hello.txt')) and have that batch script queued (with Windows scheduler or whatever the cron-equivalent is on Windows) to run in an async fashion (and clear itself at the end).