Re: Overwriting specific lines in text files without rewriting the whole thing.

From: Date: Mon, 26 Jun 2000 21:43:30 +0000
Subject: Re: Overwriting specific lines in text files without rewriting the whole thing.
Groups: php.general 
Request: Send a blank email to php-general+get-3213@lists.php.net to get a copy of this message
John, Suggestion #2: $length=9; // largest possible # of digits in counter (set to what you want) $id=$unique_id_for_this_page; // first id should be 0 $fp=fopen("/path/to/counterfile","+r"); fseek($fp,$length * $id); // position to counter for this page flock($fp,2); // exclusive lock on file $count=fread($fp,$length); // get it fseek($fp,$length * $id); // re-position fwrite($fp,sprintf("%${length}d",$count + 1); // put new counter back, hopefully with a fixed length flock($fp,3); // release lock fclose($fp); Assumptions: 1) You can assign a unique integer each page 2) Counter file exists and is filled with enough zeros to allow for all counters. If you have 1100 pages, then you must have at least 9 * 1100 = 9900 zeros. 3) flock($fp,2) will wait for a lock if someone else has locked the file 4) All my syntax is correct (a drastic assumption, especially on the fwrite() line) HTH again, Carroll Barrett -----Original Message----- From: John Biesnecker <amraam@ao.net> To: php-general@lists.php.net <php-general@lists.php.net> Date: Monday, June 26, 2000 4:19 PM Subject: Re: [PHP-GENERAL] Overwriting specific lines in text files without rewriting the whole thing. >Unfortunately, I have something like 1100 pages, if you count all of the >dynamically generated pages... I'm sure it's possible, and I think I could >do it in C or something, but I've looked over the PHP filesystem functions a >bunch and can't see it... > >/s/ John > >----- Original Message ----- >From: Carroll Barrett <cbarrett@inalt.com> >To: <php-general@lists.php.net> >Sent: Monday, June 26, 2000 5:02 PM >Subject: Re: [PHP-GENERAL] Overwriting specific lines in text files without >rewriting the whole thing. > > >> John, >> >> You might consider having a separate counter file for each page, >> if you don't have too many pages. >> >> Regards, >> Carroll Barrett >> >> -----Original Message----- >> From: John Biesnecker <amraam@ao.net> >> To: php-general@lists.php.net <php-general@lists.php.net> >> Date: Monday, June 26, 2000 3:48 PM >> Subject: [PHP-GENERAL] Overwriting specific lines in text files >> without rewriting the whole thing. >> >> >> I have created rather simple file-based page hit counter for my >> site (because I can't use SQL or anything else). Currently I am >> reading the entire thing into an array using file(), finding the >> correct line, editing it and writing the entire thing back into a >> file. >> >> After a few weeks of content development, however, my counter >> data file has gotten very large, and reading and writing the >> entire file is beginning to take a noticable amount of time. I'm >> already sorting the file so the most accessed pages are on top, >> hoping to reduce time needed to find the correct entry. >> >> I'm wondering if there is a way that while reading the file line >> by line and I can alter a single line and write it back to the >> file without disturbing the rest of the lines and then exit. >> Also, if there is, will I see any performance improvement worth >> the code rewrite. >> >> Thanks. >> >> >> >> -- >> PHP General Mailing List (http://www.php.net/) >> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net >> For additional commands, e-mail: php-general-help@lists.php.net >> To contact the list administrators, e-mail: php-list-admin@lists.php.net >> > > >-- >PHP General Mailing List (http://www.php.net/) >To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net >For additional commands, e-mail: php-general-help@lists.php.net >To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#3213) next »