file_set_contents() do several times?

file_set_contents() do several times?

am 20.07.2009 02:46:23 von Martin Zvarik

$i = 0;
do {
$i++;
$r = file_put_contents('file.txt', 'content');
} while($r === false && $i < 3);

if ($r === false) die('error');

?>

Makes sense? or is it enough to do it just once?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: file_set_contents() do several times?

am 20.07.2009 05:31:21 von List Manager

Martin Zvarík wrote:
> > $i = 0;
> do {
> $i++;
> $r = file_put_contents('file.txt', 'content');
> } while($r === false && $i < 3);
>
> if ($r === false) die('error');
>
> ?>
>
> Makes sense? or is it enough to do it just once?
>

You call file_put_contents() once.

it writes an entire file when called.

use fopen/fwrite/fclose to do it line by line.

--
Jim Lucas

"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: file_set_contents() do several times?

am 20.07.2009 10:16:15 von David Otton

2009/7/20 Martin Zvar=EDk :
>
> > $i =3D 0;
> do {
> =A0 $i++;
> =A0 $r =3D file_put_contents('file.txt', 'content');
> } while($r ===3D false && $i < 3);
>
> if ($r ===3D false) die('error');
>
> ?>
>
> Makes sense? or is it enough to do it just once?

Assuming 'content' changes, and this is the cut-down example...

$r =3D file_put_contents('file.txt', 'content', FILE_APPEND);

There's a small overhead in opening/closing the file three times
instead of one, but I doubt that's going to be an issue for you.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: file_set_contents() do several times?

am 20.07.2009 10:48:15 von Martin Zvarik

David Otton napsal(a):
> 2009/7/20 Martin Zvarík :
>> >> $i = 0;
>> do {
>> $i++;
>> $r = file_put_contents('file.txt', 'content');
>> } while($r === false && $i < 3);
>>
>> if ($r === false) die('error');
>>
>> ?>
>>
>> Makes sense? or is it enough to do it just once?
>
> Assuming 'content' changes, and this is the cut-down example...
>
> $r = file_put_contents('file.txt', 'content', FILE_APPEND);
>
> There's a small overhead in opening/closing the file three times
> instead of one, but I doubt that's going to be an issue for you.
>


I am not appending anything, just ensuring that it will be written (and
giving it max. 3 tryes).


=== Martin Scotta replied:

In my experience file_put_contents never fails, except about file
permissions, but this is not the case.

---

Thanks for your replies.

Martin

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: file_set_contents() do several times?

am 20.07.2009 11:31:24 von David Otton

2009/7/20 Martin Zvar=EDk :

>> 2009/7/20 Martin Zvar=EDk :
>>> >>> $i =3D 0;
>>> do {
>>> =A0 $i++;
>>> =A0 $r =3D file_put_contents('file.txt', 'content');
>>> } while($r ===3D false && $i < 3);
>>>
>>> if ($r ===3D false) die('error');
>>>
>>> ?>
>>>

> I am not appending anything, just ensuring that it will be written (and
> giving it max. 3 tryes).

Ok, don't do that. If it didn't work the first time, it won't work
0.001 seconds later, either. Chances are it will either be a
permissions error, or out-of-disc-space.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php