Hello 10,000 Screaming Monkeys,
(Sorry, I couldn't resist!)
You should test the return value of flock(), not just run it. It is
designed to tell you whether or not something is safe to do, and since
you are not testing the result, you are simply ignoring what it is
trying to tell you.
I have a small script that uses flock():
http://www.nirvani.net/software/dr-fun/
As you can see, I am testing the result, and taking an action (in this
case, sleeping, then trying again).
Hope this helps.
Jeremy
Jeremy Brand :: Sr. Software Engineer :: 408-245-9058 :: [EMAIL PROTECTED]
http://www.JeremyBrand.com/Jeremy/Brand/Jeremy_Brand.html for more
Get your own Free, Private email at http://www.smackdown.com/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"<body>" -- Quoted from Yahoo! homepage, http://www.yahoo.com
On Sat, 13 Jan 2001, 10,000 Screaming Monkeys wrote:
> Date: Sat, 13 Jan 2001 17:54:31 -0600
> From: "10,000 Screaming Monkeys" <[EMAIL PROTECTED]>
> To: [EMAIL PROTECTED]
> Subject: [PHP] problem using flock()
>
> 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]
>
>
--
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]