0

I have an upload script that uploads images to a server. I need a script to run in the background after the image is uploaded and moved by move_uploaded_file(), but I need to send a filename and path to that file to the script (pretty much send some data).

How can I run a PHP script in the background after some data has been sent to it?

Notes: The files are uploaded via XMLHttpRequest.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
tundoopani
  • 255
  • 9
  • 21

1 Answers1

1

First read this

php exec command (or similar) to not wait for result

It is basically pointing out that you need to create a script that takes two params ( filename, path)

Then you need to use the exec function

Edit:

As pointed out by Brent Baisle in the comments below, "nohup" must be added infront of the script to execute independently (with a different parent pid)

exec('noup php scriptyoucreated.php '.$filename.' '.$path.' > /dev/null 2>&1 &');

This writes the output to dev/null, php should not block at this point, i have not tried, but in theory.

Community
  • 1
  • 1
Bodman
  • 7,938
  • 3
  • 29
  • 34
  • I believe exec() will indeed block. You could fork your process: http://php.net/manual/en/function.pcntl-fork.php – 0x6A75616E Sep 14 '11 at 01:50
  • exec() only blocks if it is waiting for a response. Redirecting the output makes it unblocking. However, you need to include "nohup" in front the script you are running. Otherwise the script will die when the parent process ends. You don't want to fork under Apache. – Brent Baisley Sep 14 '11 at 02:06