PDA

View Full Version : flock is too good?


gorilla1
09-19-2002, 11:41 PM
I am running the code below on phpdev. If I leave out the lock logic, the file gets written. However, with the lock logic, it does not. I tried fputs, too, and no success. Can anyone spot where I have erred?

G

$fp = fopen('emails.txt','a');
$lock = flock($fp, LOCK_EX);
if($lock){
$emailsize = strlen($email . "\n");
$fw = fwrite($fp, $email . "\n", $emailsize);
if ($fw)
echo "<b><div align=center>$email has been added to the subscriber list.</div></b>";
else
echo "Error!";

flock($fp, LOCK_UN);
if(!fclose($fp)) {
echo "Error closing file!";
exit;
}
}

firepages
09-20-2002, 02:28 AM
flock is ignored on windows... so whilst using flock() will not in itself cause any problems as PHP ignores it on winn32 ... this is probably the problem.


$lock = flock($fp, LOCK_EX);
if($lock){ ...

here you are testing for $lock, which will be false as the flock() function has been ignored...

workaround ... errr of the top of my head you could test for OS but I am sure there is a better way?

if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN'){// etc

gorilla1
09-20-2002, 04:19 AM
firepages,

Ah, ok, thanks.

G

Spookster
09-20-2002, 05:50 AM
Hmmm. I've been running phpdev on my WinXP system and flock works just fine.

Here is a script I wrote awhile ago which works just fine:



<?php
$file_name = "accesslog.txt";
$data = date("l, d-M-Y H:i:s T").",".$REMOTE_ADDR.",".$HTTP_USER_AGENT."\n";

$file_pointer = fopen($file_name, "a");
$lock = flock($file_pointer, LOCK_EX);

if ($lock){
fputs($file_pointer, $data);
flock($file_pointer, LOCK_UN);
}
fclose($file_pointer);
?>