PHP, Windows, and the exec() function
I've been developing a little web app that allows someone to remotely play MP3s by sending commands to a Windows server running Apache2.2, PHP5, and foobar2000 (the latter being the MP3 player)
For some reason over the course of this week, all three installations of this app I have stopped working. The problem was coming from PHP all of a sudden deciding that it wanted to wait for the execution of the program before finishing its process. I'm still not sure why all three machines stopped working (I haven't touched the code for ~5 days until today) especially given that one is a Vista Ultimate x64, another is XP SP2 x86, and the main one being a Server 2003 box.
Anyway, the original way I was handling sending commands was using the Windows 'start' command to pass the actual program call. Using 'start' allows you to run programs that won't necessarily close right after execution (ie foobar2000). Example of the old way:
But as I said, it wasn't working anymore as PHP would hang on that particular host until I would manually RDP into the server and kill the foobar2000 process from the task manager.
After I went through as many different ways to stop this from happening, including creating a batch file that just ran foobar2000 and having that called by 'cmd.exe' which is then in turn called by 'start', I finally came up with something that worked thanks to a Russian web site that I couldn't understand save for the PHP examples:
Basically, it uses popen() to actually open the program (as well as create a handle inside PHP) and then immediately release the handle with pclose(). Now I'm again happily sending commands to foobar2000.
September 7th, 2007 at 9:33 am
Oh wait. Yes, I have. I'm sorry, but I just don't have it in me right now to type it all out again. Besides, it was just ramblings anyway. You didn't want to hear me go on and on about this, right?
September 7th, 2007 at 11:24 am
Well, sure!