2

I have the following situation: A remote process called "A" sends files to a FTP server. A remote process called "B" reads the files sent by the process "A" in the FTP server and do something.

This situation illustrates a potential concurrency problem if the process "B" reads a file is being writing by the process "A".

I need to develop the process "B" in PHP. Can I check if a file is being writing right now? but if not... What alternative do you suggest?

EDIT Process A is not under my control.

Cristhian Boujon
  • 4,060
  • 13
  • 51
  • 90

2 Answers2

0

You can check the filesize of the file that's possible being written (with filesize()), sleep 5 seconds and check the filesize again. If it's changed, you know the file is still being written. If not, you can assume the file is uploaded.

It's not foolproof, but as far as I know there's no other solution to check this with PHP.

TrafeX
  • 1
  • 2
    This certainly isn't true. Just because you sleep the process, it doesn't mean that the process that is doing the writing is 100% definitely going to run. – Patrick James McDougle Jun 04 '14 at 19:23
  • @PatrickJamesMcDougle That's absolutely true. As I said, it's not foolproof, but it can help in some cases. – TrafeX Jun 05 '14 at 06:44
0

Assuming Process A is under your control.

Suppose processA upload the file with a temporary name. Once uploading is completed Process A rename it back. Process B is looking only for the real files. So you don't worry about the uploading file read by the Process B

fortune
  • 3,361
  • 1
  • 20
  • 30