Good day. On Windows XP (FAT32), I wish to write something on a text file and to avoid other people from writing on it at the same time, I used flock.

$tmp_file = fopen("temp_log.txt", "ab");
flock($tmp_file, 2);
fwrite($tmp_file,"line of text");

How I try out is: Immediately after locking the temp_log.txt, I let one of the script sleep for few seconds. Then I run another script which also want to write into temp_log.txt.

Surprisingly, it can be written although it's locked.

I heard "flock" cannot be used on Windows. Is it true? Which version of Windows are they?

Also, do we have to unlock the file by using flock("filename", 3)? Or, if we call fclose, the lock will be automatically released?

Thanks and hope to hear from you all soon.

    When in doubt, always look at the documentation 🙂

    It says there that flock won't work on filesystems like FAT (I guess that includes FAT16 and FAT32). Which brings me to my question: Why are you using FAT with WinXP? NTFS is better than FAT...

    Diego

      Thanks for the reply. Actually the reason for me to use FAT32 is because there's Win98 on the same hard disk as well. So for convenience sake I use FAT32 for all partitions.

      Perhaps it's time to try out NTFS and see if it's working or not.

        File locking is just sketchy in Windows period, regardless of filesystem. If you want to use Nix functions you should use a Nix OS.

          Write a Reply...