Thread: flock()

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

    Join Date
    Jun 2000
    Posts
    22
    Rep Power
    0
    Hi again, My script= <? $fd=fopen("MyTest.txt","r+"); //read/write
    flock($fd,2); $MyCount=fread($fd,filesize("MyTest.txt"));
    $MyCount=intval($MyCount); $MyCount=$MyCount+100;
    fputs($fd,$MyCount); fclose($fd); ?>
    This opens the file reads the value and rewrites the new value BUT I notice that it
    -appends- ie orig num = 111211 + 100 = 111311 but in file I have
    111211111311.
    Do I need to close then reopen the file or is there another command to use?
    Thanks heaps, **** Title misleading - shd be (maybe) how to stop appending> ****

    ------------------
    With my kind regards,
    Peter.

    [This message has been edited by pjlewis (edited July 10, 2000).]
  2. #2
  3. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15
    Hi,

    just try this in a different way.
    Read the value from the file first.then again open it in write mode and just write the new value to it..


    try something like following:

    <?
    $fd=fopen("MyTest.txt","r");
    $MyCount=fread($fd,filesize("MyTest.txt"));

    //$MyCount=intval($MyCount);
    echo $MyCount;
    fclose($fd);


    $fh=fopen("MyTest.txt","w");
    $count=$MyCount+100;
    fputs($fh,$count);
    fclose($fh);

    ?>

    i have not tested this ..Any way give a try..

    Good Luck!!!



    ------------------
    SR -
    webshiju.com

    "The fear of the LORD is the beginning of knowledge..."
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    22
    Rep Power
    0
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by Shiju Rajan:
    Hi,

    just try this in a different way.
    Read the value from the file first.then again open it in write mode and just write the new value to it..


    try something like following:

    <?
    $fd=fopen("MyTest.txt","r");
    $MyCount=fread($fd,filesize("MyTest.txt"));

    //$MyCount=intval($MyCount);
    echo $MyCount;
    fclose($fd);


    $fh=fopen("MyTest.txt","w");
    $count=$MyCount+100;
    fputs($fh,$count);
    fclose($fh);

    ?>

    i have not tested this ..Any way give a try..

    Good Luck!!!
    Thats fine, however I wish to lock the file so no one else can access between read/write. Just in Case!!


    [/quote]

    ------------------
    With my kind regards,
    Peter.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    22
    Rep Power
    0
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by Shiju Rajan:
    ok..

    use flock() function before writing to the file..

    $fh=fopen("MyTest.txt","w");
    flock($fh,2);
    //exclusive lock --writer.
    $count=$MyCount+100;
    fputs($fh,$count);
    fclose($fh);

    -------


    [/quote]Ok. Am I being too paranoid? eg. You enter my site get count of 100 - Almost immediatly someone else enters and because I haven't locked it will also get a count of 100. When these are written to the file the result will be 200 for you and 200 for other uses. I want 200 for you and 300 for other.

    ------------------
    With my kind regards,
    Peter.
  8. #5
  9. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15
    sorry,
    A small change in my previous post...


    you should remove the lock beofore writing in to the file.after writing just put the exclusive lock..

    i am not sure the locking and releazing numbers.just chck it out the manual...


    $fh=fopen("MyTest.txt","w");

    flock($fh,3);
    //release a lock ...

    $count=$MyCount+100;
    fputs($fh,$count);

    flock($fh,2);
    //exclusive lock --writer.

    fclose($fh);



    ------------------
    SR -
    webshiju.com

    "The fear of the LORD is the beginning of knowledge..."

Similar Threads

  1. php perl flock function
    By jeremy143 in forum PHP Development
    Replies: 0
    Last Post: November 3rd, 2003, 03:57 AM
  2. flock not working on local server
    By hardseeker in forum Perl Programming
    Replies: 11
    Last Post: October 17th, 2002, 11:14 AM
  3. flock() : Must I use it if I'm only appending to a file?
    By PhatBarren in forum PHP Development
    Replies: 0
    Last Post: June 23rd, 2002, 08:21 PM
  4. Two more flock questions
    By jjohnson81 in forum Perl Programming
    Replies: 1
    Last Post: April 7th, 2002, 09:54 AM
  5. Why dosn't this simple 'flock()' test work?
    By jjohnson81 in forum Perl Programming
    Replies: 2
    Last Post: April 4th, 2002, 11:22 PM

IMN logo majestic logo threadwatch logo seochat tools logo