Wait until file is written to

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wellhole
Forum Newbie
Posts: 16
Joined: Mon Mar 23, 2009 1:38 pm

Wait until file is written to

Post 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?
Last edited by wellhole on Mon Mar 23, 2009 3:26 pm, edited 2 times in total.
atonalpanic
Forum Commoner
Posts: 29
Joined: Mon Mar 02, 2009 10:20 pm

Re: Wait until file is written to

Post 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.
wellhole
Forum Newbie
Posts: 16
Joined: Mon Mar 23, 2009 1:38 pm

Re: Wait until file is written to

Post 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.
atonalpanic
Forum Commoner
Posts: 29
Joined: Mon Mar 02, 2009 10:20 pm

Re: Wait until file is written to

Post 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.
wellhole
Forum Newbie
Posts: 16
Joined: Mon Mar 23, 2009 1:38 pm

Re: Wait until file is written to

Post 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.
atonalpanic
Forum Commoner
Posts: 29
Joined: Mon Mar 02, 2009 10:20 pm

Re: Wait until file is written to

Post by atonalpanic »

Edit: reread your post. let me think about it.
atonalpanic
Forum Commoner
Posts: 29
Joined: Mon Mar 02, 2009 10:20 pm

Re: Wait until file is written to

Post 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.
wellhole
Forum Newbie
Posts: 16
Joined: Mon Mar 23, 2009 1:38 pm

Re: Wait until file is written to

Post by wellhole »

I agree thats quite a hack. heh.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Wait until file is written to

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
wellhole
Forum Newbie
Posts: 16
Joined: Mon Mar 23, 2009 1:38 pm

Re: Wait until file is written to

Post by wellhole »

That's possible... I'll do something similar to that. Thanks!
tech603
Forum Commoner
Posts: 84
Joined: Thu Mar 19, 2009 12:27 am

Re: Wait until file is written to

Post 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);
}
Post Reply