Thread: F Locking

    #1
  1. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    6
    Rep Power
    0
    when do you guys advise to use flock? I have a script that writes into a .txt file Everytime a user comes on my page

    This is the code i use to write: not much special
    $hits is an array wich includes the complete .txt file and is writen (changed) back into that .txt file
    Should i use flock or not?

    $fd = fopen($filename , "w");
    for($b=0 ; $b<count($hits) ; $b++)
    {
    fwrite($fd, $hits[$b]);
    }
    fclose($fd);

    thnx



    ------------------
    Until next time, I'll Remain!!
    Quote the Dokus:"NeverMore!"
  2. #2
  3. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,642
    Rep Power
    4492
    You should use a database, actually.

    If you can't use a database, then flock could help some. The problem comes when two people come to your site at the same time and try to access your .txt file. Will the one request just wait while the other reads, or will it give an error? Also, if one request reads the file, makes the changes, then a second request read the file, makes the changes, then the first request writes, then second request writes...you're basically missing one of the requests, so you're information wouldn't be accurate.

    ---John Holmes...
  4. #3
  5. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    6
    Rep Power
    0
    Ok i can see that problem

    but what if someone writes into the .txt file, and locks it, and someone else wants to write into the .txt file! will it give an error? or will it place him in a wait? and as soon as the .txt file is being unlocked the second person writes to the file?

    how does that work with flock?

    i have a high traffic site, and i don;t mind inaccurate stats. but i can;t afford to have errors

    thnx for the reply btw

    ------------------
    Until next time, I'll Remain!!
    Quote the Dokus:"NeverMore!"
  6. #4
  7. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,642
    Rep Power
    4492
    If you have a high traffic site, then you REALLY need to look into getting a database installed. Install MySQL, it's free and already integrated into PHP. It's easy to learn, also.

    I read the manual page on flock() and it says to use flock() just before you write to a file. A comment on the page says that if the server encounters a locked file, it waits until it is unlocked to read it.

    You'll have to play around with it to get it to work right. I seems like this would slow you page down considerably if everyone is waiting for the file to be unlocked from the previous user...

    ---John Holmes...

Similar Threads

  1. Replies: 6
    Last Post: July 21st, 2004, 03:53 PM
  2. My Sql innodb row-level locking question
    By jusan in forum MySQL Help
    Replies: 5
    Last Post: May 1st, 2003, 05:16 PM
  3. Row level locking with InnoDB
    By mydevshedacc in forum MySQL Help
    Replies: 1
    Last Post: January 14th, 2003, 04:41 PM
  4. File locking in ASP
    By robert.swift in forum Windows Help
    Replies: 2
    Last Post: August 29th, 2002, 04:04 PM
  5. locking rows
    By jhiza in forum MySQL Help
    Replies: 2
    Last Post: April 20th, 2002, 10:02 PM

IMN logo majestic logo threadwatch logo seochat tools logo