views:

22

answers:

2

I have two processes running in different scripts indepdent of each other.

  1. PHP #1 script reads the filename of a file and writes it to a DB
  2. PHP #2 script uploads the file

I would like to create a third script (also independent) that only executes once script #2 has fully completed uploading the file.

I'm thinking of using PHP's flock function to determine if the file is locked. I imagine script #3 would need to contain some sort of flock check function that was set on a timer to check to see if the file was unlocked, and if so to proceed.

Note: script #3 would know the filename to perfom the check

Any ideas? It would be a lot easier if the files could be chained, but they execute indepently.

A: 

is script 2 in a web facing page? if so, I would fire back a AJAX call to the server to run script 3. The page can't render until the script is done, and once it renders it can fire back the call on page load...

FatherStorm
i may be able to restructure it a bit to accomplish something like this.
Tegan Snyder
A: 

I don't know if the file is locked all the time while it's being written to so I wouldn't rely on that.

I would check for changes in the file size at intervals (in conjunction with flock). And when the files stops growing consider that it finished uploading.

If you have control over the 2nd script just trigger the 3rd one when it finishes execution (``). Or if that is not possible use a file or something to feed the upload state from #2 and to read it from #3.

Alin Purcaru
I think if I just had a efficient way to check if the file exists every 5 seconds or so in a loop. But I would need the timer to have a max time so it isnt an infinite loop.
Tegan Snyder
<?php $time = 60; $found = false; for($i=0; $i<$time; $i++){ if(file_exists($filename)){ $found = true; break; } sleep(1); } if($found) { //do next task } else { //timed out } ?>
Tegan Snyder
I don't understand where the problem is. You waiting script looks fine.
Alin Purcaru