0

I have read the PHP manual and understand that flock may not work in FAT environment.

<?php

// assign file
$file = "testfile";
$content = "test";

// check whether file is lock? If so, wait.
while(file_exists($file . ".lock"))
{
    usleep(500000);
}

if (!file_exists($file . ".lock"))
{
    // lock the file before writing to file
    $fl = fopen("$file.lock","w");
    fwrite($fl,"");

    // write to file
    $fp = fopen("$file", "r+");
    fwrite($fp, $content);
    fclose($fp);

    // unlock file
    fclose($fl);
    unlink($file . ".lock");
}

?>

Would you do anything differently? What would you change? Thanks!

Ting Ping
  • 1,145
  • 7
  • 18
  • 34
  • Symfony just release its Lock component, https://github.com/symfony/lock/blob/master/Store/FlockStore.php, no need to reinvent the wheel – Federkun Mar 23 '17 at 12:40
  • Sorry, I am not an expert in PHP. Trying to implement it in my local area network which is using a fat system. Does that work? – Ting Ping Mar 23 '17 at 12:41
  • your approach seems pretty straight forward, why would it not work? anyway who still uses FAT? :D – xander Mar 23 '17 at 12:43
  • I'm implementing my codes in a local area network supported by Synology NAS which is running on the FAT file system. Thanks for your comment. :) – Ting Ping Mar 23 '17 at 12:45

0 Answers0