I did this a while ago... It required the use of a special C program function that I found somewhere on the internet...
#1, you MUST compile the C program first.
Here is the C program. It's really simple, if you know C ;)=
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int pid;
cout << "BGSTART: command [arguments]" << endl;
cout << "Number of arguments: " << argc;
for( int counter = 0; counter < argc; counter++ ) {
cout << " argc[" << counter << "]: " << argv[ counter ] << endl;
}
cout << "Forking off..." << endl;
pid=fork();
if (pid!=0)
{
//executing the program passed in on the command line
close(STDOUT_FILENO);
close(STDIN_FILENO);
close(STDERR_FILENO);
execvp(argv[1], &argv[1]);
// http://www.php.net/manual/ref.exec.php3
// http://home.i1.net/~naken/mikehup.c
}
return 0;
}
I'm not that great at C, and I don't remember if this is the version of the code that I compiled, but it's the only thing I have in my directory, so it's what I'm spitting out at you. I called it 'bgstart', you can call it whatever you'd like.
#2, use it in your php code like this:
<?PHP
$mp3_to_play = EscapeShellCmd( $mp3_to_play );
$command = "/usr/local/bin/bgstart /usr/bin/mpg123 $mp3_to_play &";
?>
#3, you can see it in action here:
http://24.17.173.98/~rames/audio/cdplayer/music.php
I just upgraded from php3->php4 at home, so if you click around to some of the other stuff, i'm sure you'll see some broken things, but feel free.
#4, the reason for all this...
PHP is kindof cool in that it will wait for a process to finish before executing the next line of code. It allows you to 'exec' system commands just as if they were a line of PHP code. But that's the problem you're trying to get around- you don't want to wait for that process to finish, you want to let the computer do it's whole computing thing while the user gets on with their life.
What 'bgstart' does is start up really quick, use fork to make another copy of itself (which php doesn't know about). Within that new process, it runs your processor-intensive code (and php doesn't know about it), and then it just executes a "return(0)" for the process that php does know about.
PHP sees that the process it launched (bgstart) terminated, while the other program (in my case, mpg123) continues running merrily. PHP then returns control to the next command in your php script because 'bgstart' has already terminated.
Hope this helps, and I hope that the c proggie i gave you actually does compile. :)=
--Robert