Re: problem using flock()

From: Date: Sun, 14 Jan 2001 05:37:21 +0000
Subject: Re: problem using flock()
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-34450@lists.php.net to get a copy of this message
Oh man, I wouldn't do it this way. Log this stuff in a separate file using a simple append which doesn't need logging. Then write yourself a simple little perl script that figures it out after the fact. File locking on anything with high traffic is going to make your hair turn grey. Another alternative is to log this stuff to a database. -Rasmus On Sat, 13 Jan 2001, 10,000 Screaming Monkeys wrote: > Hi, > > I'm having a problem with one of the PHP scripts I've written and I'm > hoping someone can point me in the right direction. The portion of > the script that is giving me trouble is the locking of, and writing to, > a logfile (plain text). I'm using flock() as I understand it and have > looked at the online manual page for it to no avail... > > What's happening is, under "high" traffic conditions on the website I > wrote the script for, the logfile is occasionally getting clobbered. I > tried one of the suggestions on the man page (the one by > paolo@mailgate.net), but it resulted in the logfile being appended to, > rather than properly updated as I want. (better than being clobbered, > but rather confusing to cleanup after later...) > > Here is an example snippit of the script. Can anyone tell me what I > might be doing wrong, or suggest a better/more efficient way to write > this? > > <BEGIN CODE SNIP> > if (@fopen($dlfile, "r")) { > > $log_entries = file($download_log); > $total = count($log_entries); > $dl_time = time(); > > for ($i=0; $total>$i; $i++) { > $split = explode("||", $log_entries[$i]); > > if ($stats_url == $split[1]) { > // If the file being downloaded is already in the logfile, > // increment the download count and update the $dl_time > > $fp = fopen($download_log, "r"); > flock($fp,1); > $x = fread($fp, filesize($download_log)); > fclose($fp); > > $fp = fopen($download_log, "w"); > flock($fp,2); > > // Increment download counter > $add = $split[0]+1; > > // Do the actual update of the download count & latest dl time > $x = str_replace("$split[0]||$split[1]||$split[2]||$split[3]||$split[4]||", > "$add||$split[1]||$split[2]||$dl_time||$split[4]||", $x); > > fwrite($fp, $x); > > fclose($fp); > $write = 1; > } > > } > if ($write != 1) { > > // The file being downloaded is not in the logfile -- > // add it now and start its counter at 1 > > $fp = fopen($download_log, "a"); > flock($fp,2); > $fw = fwrite($fp, > "1||$stats_url||$dl_time||$dl_time||dir2=$dir2&file=$urlfile||\n"); > fclose($fp); > > } > } > <END CODE SNIP> > > Thanks...I'm desperate... > > - Jamie > > -- > The sweetest cherry in an apple pie > <php@in-flux.org> (Single purpose Email address) > > -- > 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 (#34450) next »