RE: [PHP] fopen
| From: | Lars Torben Wilson | Date: | Fri, 15 Feb 2002 09:53:13 +0000 |
| Subject: | RE: [PHP] fopen | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-84879@lists.php.net to get a copy of this message | ||
On Thu, 2002-02-14 at 18:27, Martin Towell wrote:
> or read the original file into memory, reopen the file for writing, write
> the first prepend bit, then write what you just read
The problem with that is that you have a time lag (perhaps only
in microseconds, but it still is a problem) while you're writing
the new data to the file. If some other process--say, another
invocation of the same script--tries to read from that file while
you're still overwriting it--you've got problems, since it'll find
only a partially-written file.
That's called a 'race condition'. The other method avoids that,
since while you're writing data to the temp file, processes just
keep using the original. Once you're done, the rename happens
atomically, preventing the race condition.
Torben
> -----Original Message-----
> From: Lars Torben Wilson [mailto:torben@php.net]
> Sent: Friday, February 15, 2002 1:18 PM
> To: John Smythe
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] fopen
>
>
> On Thu, 2002-02-14 at 17:34, John Smythe wrote:
> > i wanna know if there is a way of opening a file in a certian mode so when
> i write to a txt file it writes at the top of the file, and doesnt overwrite
> whats at the start of the file
>
> No, not with fopen(). What you can do is create a temporary file with
> tempnam(), write the new data to that, *append* the data from the
> original file, and rename the temp file over the original file.
>
>
> Cheers,
>
> Torben
>
>
> > Regards,
> > John Smythe
> > http://www.smythey.org/
> --
> Torben Wilson <torben@php.net>
> http://www.thebuttlesschaps.com
> http://www.hybrid17.com
> http://www.inflatableeye.com
> +1.604.709.0506
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
--
Torben Wilson <torben@php.net>
http://www.thebuttlesschaps.com
http://www.hybrid17.com
http://www.inflatableeye.com
+1.604.709.0506