Page 1 of 1

Wait until file is written to

Posted: Mon Mar 23, 2009 1:54 pm
by wellhole
I have a process that creates a file and writes text into it and it runs 24/7. The process would just "create, write, close handle". My php page triggers this and then it has to read from the file, but right now it reads too early and gets no results.

Code: Select all

while (!file_exists($fname))
  usleep(100);
// Insert code to wait until file is ready
//
//
$str = file_get_contents($fname);
print $str;
How can I make it wait until the process finishes writing to the file?

Re: Wait until file is written to

Posted: Mon Mar 23, 2009 3:12 pm
by atonalpanic
First off, I believe you should only use sleep() with an integer value.
If you want to use parts of a second, try usleep():

http://us3.php.net/manual/en/function.usleep.php

It still takes integers, but this one gets more precision.
Also the file you are checking for existence and the one
you are reading from are two different files, it seems.

Or maybe I'm missing what you are trying to do. That loop
will work file if you use it to check for the existence of the
file that you are actually reading from.

Re: Wait until file is written to

Posted: Mon Mar 23, 2009 3:25 pm
by wellhole
Thanks for the heads up on sleep(). Sorry, I made a booboo. Those 2 variables were for the same name.

Also, file_exists() will not work by itself because get_file_contents() will not wait for the file to be populated. The process created the file handle, and is processing information when get_file_contents() fires. It hasn't had a chance to flush the output or to close the file handle.

Re: Wait until file is written to

Posted: Mon Mar 23, 2009 5:50 pm
by atonalpanic
How about locking the file until the write is finished?

http://us2.php.net/flock

Then all you have to do is wait until the lock is released
to write the contents to the file.

Re: Wait until file is written to

Posted: Mon Mar 23, 2009 5:52 pm
by wellhole
The documentation for flock says this is a php implementation and will not work with other codebases. The process that creates this file is not php.

Re: Wait until file is written to

Posted: Mon Mar 23, 2009 5:59 pm
by atonalpanic
Edit: reread your post. let me think about it.

Re: Wait until file is written to

Posted: Mon Mar 23, 2009 6:22 pm
by atonalpanic
It is somewhat of a hack but I imagine that this might work...

Check the size of the file one second (or so) and compare it
to the size of the file a second later. If it doesn't change during
the time difference than you can consider it finished. It SEEMS
unlikely that the file size WOULDN'T change over the course of
a second or more unless its finished writing.

Re: Wait until file is written to

Posted: Mon Mar 23, 2009 6:58 pm
by wellhole
I agree thats quite a hack. heh.

Re: Wait until file is written to

Posted: Tue Mar 24, 2009 9:59 am
by pickle
Do the automated process & PHP run under the same user? Is it allowable to have your PHP file unable to read the file at any point in time? If the answer to both questions is "yes", just have your automated process change the user & group ownership of the file to something the web server can't read. Then, when the automated process is done, change the ownership back.

Re: Wait until file is written to

Posted: Tue Mar 24, 2009 11:24 am
by wellhole
That's possible... I'll do something similar to that. Thanks!

Re: Wait until file is written to

Posted: Tue Mar 24, 2009 10:48 pm
by tech603
You could try something like this, i've used similar for texting ajax and waiting for an element to become present. This will wait 60 seconds for the file to create then timeout, if the file is found it will break out of the loop.

for ($second = 0; ; $second++) {
if ($second >= 60) $this->fail("timeout");
try {
if ($this->file_exists($fname)) break;
} catch (Exception $e) {}
sleep(1);
}