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
[EMAIL PROTECTED]), 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
<[EMAIL PROTECTED]>  (Single purpose Email address)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to