note 52783 added to function.fwrite

From: Date: Thu, 12 May 2005 12:25:12 +0000
Subject: note 52783 added to function.fwrite
Groups: php.notes 
Request: Send a blank email to php-notes+get-89328@lists.php.net to get a copy of this message
bluevd at gmail dot com mentioned a hit counter. In his/her implementation, the file is first opened, read, closed, then opened +truncated, then written, and closed again. An alternative to this is: <?php $file = 'counter.txt or whatever'; $handle = fopen ($file, 'r+'); // Let's open for read and write $count = int (fread ($handle, filesize ($file))); // We don't want to think it's a string and try appending echo "Number of hits $count"; rewind ($handle); // Go back to the beginning /* * Note that we don't have problems with 9 being fewer characters than * 10 because we are always incrementing, so we will always write at * least as many characters as we read **/ fwrite ($handle, ++$count); // Don't forget to increment the counter fclose ($handle); // Done ?> ---- Manual Page -- http://www.php.net/manual/en/function.fwrite.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+52783 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52783&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52783&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52783&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52783&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52783&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+52783&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#89328) next »