1

I have searched extensively to solve the issue of opening an external program in phpdesktop without PHP waiting ultimately making PHP timeout.

I need to be able to launch the program with exec() and have the rest of the PHP code execute without waiting on the exec() command. I have tried multiple solutions. To make matters even more complicated the file that is being opened is on a networked drive. Here is what I have tried but has not worked

pclose(popen('start' .$File. '>NUL 2>NUL"', 'r'));   This didn't work because the drive is on the network

exec($File);              Doesn't work as it waits instead of executing the remainder of the code

system($File);            Doesn't work. Same result as exec()

exec($File > /dev/null);  Obviously doesnt work because php is on windows

The file being executed is a video file: mp4, avi or mkv. So it's opening the external video player file but like I said above PHP ultimately times out and gives an error after 30 seconds without executing the rest of the code. I just need PHP to ignore the program it opened and go on about its tasks. Any help would be greatly appreciated.

zx485
  • 28,498
  • 28
  • 50
  • 59
Micha
  • 383
  • 3
  • 14
  • it's opening a video player on the server? That seems odd. – Ctznkane525 Jan 07 '18 at 21:33
  • 1
    "This didnt work because the drive is on the network" . How do you know this is the reason? What does "didn't work" mean? What problem or error did you experience? – ADyson Jan 07 '18 at 21:34
  • phpdesktop runs locally as a desktop app so no it not opening on a server. It didnt work as windows threw up an error saying it couldnt access the network drive and up further research i found that it works fine EXCEPT when using it on a networked drive – Micha Jan 07 '18 at 21:40
  • See here https://stackoverflow.com/questions/5367261/php-exec-as-background-process-windows-wampserver-environment – Czarek Tomczak Jan 08 '18 at 08:12

2 Answers2

1

You could use something like "nircmd" which is a windows command-line utility to perform tasks and is a perfect fit for php-desktop. You can use the variety of "exec" actions "nircmd" has, so you could call whatever you want and immediately return to php. See the manual (.chm archive), under windows 7/10 you might have to "unblock" that help file (at file properties), to view the contents.

Juanga Covas
  • 315
  • 2
  • 5
  • I will definitely look into this tomorrow. Hopefully this will be the solution. I am very appreciative of your input. Thank you – Micha Jan 30 '18 at 02:54
  • Juanga you just made it all come together nicely. This was an excellent answer. – Micha Feb 11 '18 at 06:48
  • Glad to see it works for you. I've played with nircmd and php-desktop myself and found that both tools have a lot of potential together. AutoIt also comes to mind. – Juanga Covas Feb 11 '18 at 10:43
  • Yeah i pretty much had given up on php desktop as calling outside programs using exec was worthless as it would wait for them to close. But yes this should be a tool included with php desktop for sure. I will check out AutoIt as well. – Micha Feb 11 '18 at 17:26
  • Juanga I have successfully integrated nircmd into php desktop. It works flawlessly! I just use the nircmd.exe shexec "open" command in php and it is smooth sailing. I owe you big time! – Micha Feb 18 '18 at 04:19
  • `$CmdExe = 'nircmd.exe shexec "open" "'. $MovieHostDrive . $MovieName .'"';` `exec($CmdExe);` You have to however call the file using \\\\Machine IP Here\\DIR as it will not work with a network drive letter – Micha Feb 18 '18 at 04:26
  • You're welcome. So that would open the default "movie" player for the file format (extension of file). Thanks for the feedback! I'll be doing some php-desktop stuff real soon. – Juanga Covas Feb 18 '18 at 04:32
  • Yes for whatever file type you are targeting it opens it using the default program you have specified for the that file type in windows. Basically whats happening is the php exec is opening a hidden command prompt and then passing off the work load to nircmd in the blink of an eye. No more timeouts and no more bs. I toyed with a ton of the commands nircmd has. it was a trip to restart my PC using php. It can do almost anything you can think of – Micha Feb 18 '18 at 05:21
  • Added a section to KnowledgeBase document: https://github.com/cztomczak/phpdesktop/wiki/Knowledge-Base#how-do-i-call-external-programs-or-commands-asynchronously-in-background-without-waiting-for-them-to-end – Czarek Tomczak Mar 28 '18 at 11:48
  • Czarek, glad to see you consider the tool worth it. Some nice recipes could emerge from this (like 'bringing up' a minified php-desktop, etc) just using the lot of stuff nircmd enables to do – Juanga Covas Mar 28 '18 at 19:26
0

If 'phpdesktop' uses the built in web server in php (php -S) then it is as far as I know not possible to do so without having the exec call blocking, same with proc_open etc

I looked it up and indeed phpdesktop uses the builtin web server: https://github.com/cztomczak/phpdesktop/blob/c00988f69348b73b6dee27bdf45d145b719e2a3d/phpdesktop-chrome/php_server.cpp

In theory proc_open should work, but it doesn't

EJTH
  • 2,178
  • 1
  • 20
  • 25