Re: incrementing a number from a text file
| From: | Jason Barnett | Date: | Wed, 09 Mar 2005 23:50:21 +0000 |
| Subject: | Re: incrementing a number from a text file | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-210401@lists.php.net to get a copy of this message | ||
Ross Hulford wrote:
> I want to read a number from an external (txt) file and increment it.then
> save the number back on the text file.
> I know this is possible but want a simple amd economical way to do this.
>
> Thanks,
>
>
> Ross
Not 100% sure what the most economical way is going to be, but I'm
guessing your best bet might be:
- read in the text file with file_get_contents() to the buffer
- preg_replace() the incremented number in buffer
- file_put_contents() the buffer to overwrite the file
Of course this method assumes that there will only ever be one user
making one write at one time... so if this is part of any public web
script then what I just suggested is "A Bad Idea" (tm). But it's simple
and economical (file_*_contents uses memory mapping techniques to
improve performance... if possible on your system) and useful for
administration tasks.
If this is a public web script that needs to modify the file(s), then
you need some type of file locking for that. The easiest way to do that
is with flock(), but be certain to read the manual page before you try
to go that route:
http://php.net/manual/en/function.flock.php
--
Teach a man to fish...
NEW? | http://www.catb.org/~esr/faqs/smart-questions.html
STFA | http://marc.theaimsgroup.com/?l=php-general&w=2
STFM | http://php.net/manual/en/index.php
STFW | http://www.google.com/search?q=php
LAZY |
http://mycroft.mozdev.org/download.html?name=PHP&submitform=Find+search+plugins
Attachment: [application/pgp-signature] OpenPGP digital signature signature.asc
Attachment: [application/pgp-signature] OpenPGP digital signature signature.asc