PHP instances writing to the same file?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • abrtlt@yahoo.com

    #1

    PHP instances writing to the same file?

    I read in Programming PHP (O'Reilly) that flock() "cannot prevent two
    PHP scripts running in the same web server process from accessing a
    file at the same time".
    In my case a single PHP script appends text strings to an existing text
    file. Several clients may trigger the same script on the same web page
    and thus more than one PHP instance might try to open and append text
    to the same file at the same time. I would assume that using flock()
    _does_ in fact prevent simultaneous access in this case. Am I correct?
    Thanks!

    Andrew

  • Henrik Hansen

    #2
    Re: PHP instances writing to the same file?

    abrtlt@yahoo.co m writes:
    [color=blue]
    > I read in Programming PHP (O'Reilly) that flock() "cannot prevent two
    > PHP scripts running in the same web server process from accessing a
    > file at the same time".
    > In my case a single PHP script appends text strings to an existing text
    > file. Several clients may trigger the same script on the same web page
    > and thus more than one PHP instance might try to open and append text
    > to the same file at the same time. I would assume that using flock()
    > _does_ in fact prevent simultaneous access in this case. Am I correct?[/color]

    You are correct

    There are different types of locks though but LOCK_EX should de what
    you want..

    --
    Henrik Hansen

    Comment

    • Chung Leong

      #3
      Re: PHP instances writing to the same file?

      abrtlt@yahoo.co m wrote:[color=blue]
      > I read in Programming PHP (O'Reilly) that flock() "cannot prevent two
      > PHP scripts running in the same web server process from accessing a
      > file at the same time".
      > In my case a single PHP script appends text strings to an existing text
      > file. Several clients may trigger the same script on the same web page
      > and thus more than one PHP instance might try to open and append text
      > to the same file at the same time. I would assume that using flock()
      > _does_ in fact prevent simultaneous access in this case. Am I correct?
      > Thanks!
      >
      > Andrew[/color]

      What is meant I think is that flock() does not work on multithreaded
      environments like Windows, where all multiple PHP instances run inside
      the same process.

      Comment

      • jon.tjemsland@gmail.com

        #4
        Re: PHP instances writing to the same file?

        What OS is the server that this script is running on? Flock will work
        to lock a file if you are running your script on a server with an OS
        that is compatible with flock. Both NFS and FAT file systems are
        incompatible.

        Here is a flock sample from (http://us3.php.net/flock):

        $fp = fopen("/tmp/lock.txt", "w+");
        if (flock($fp, LOCK_EX)) { // do an exclusive lock
        fwrite($fp, "Write something here\n");
        flock($fp, LOCK_UN); // release the lock
        } else {
        echo "Couldn't lock the file !";
        }
        fclose($fp);


        Regards

        Jon Tjemsland


        abrtlt@yahoo.co m wrote:[color=blue]
        > I read in Programming PHP (O'Reilly) that flock() "cannot prevent two
        > PHP scripts running in the same web server process from accessing a
        > file at the same time".
        > In my case a single PHP script appends text strings to an existing text
        > file. Several clients may trigger the same script on the same web page
        > and thus more than one PHP instance might try to open and append text
        > to the same file at the same time. I would assume that using flock()
        > _does_ in fact prevent simultaneous access in this case. Am I correct?
        > Thanks!
        >
        > Andrew[/color]

        Comment

        • abrtlt@yahoo.com

          #5
          Re: PHP instances writing to the same file?

          Thanks. It's a server running on Linux.

          Andrew


          jon.tjemsland@g mail.com ha scritto:
          [color=blue]
          > What OS is the server that this script is running on? Flock will work
          > to lock a file if you are running your script on a server with an OS
          > that is compatible with flock. Both NFS and FAT file systems are
          > incompatible.
          >
          > Here is a flock sample from (http://us3.php.net/flock):
          >
          > $fp = fopen("/tmp/lock.txt", "w+");
          > if (flock($fp, LOCK_EX)) { // do an exclusive lock
          > fwrite($fp, "Write something here\n");
          > flock($fp, LOCK_UN); // release the lock
          > } else {
          > echo "Couldn't lock the file !";
          > }
          > fclose($fp);
          >
          >
          > Regards
          >
          > Jon Tjemsland
          >
          >
          > abrtlt@yahoo.co m wrote:[color=green]
          > > I read in Programming PHP (O'Reilly) that flock() "cannot prevent two
          > > PHP scripts running in the same web server process from accessing a
          > > file at the same time".
          > > In my case a single PHP script appends text strings to an existing text
          > > file. Several clients may trigger the same script on the same web page
          > > and thus more than one PHP instance might try to open and append text
          > > to the same file at the same time. I would assume that using flock()
          > > _does_ in fact prevent simultaneous access in this case. Am I correct?
          > > Thanks!
          > >
          > > Andrew[/color][/color]

          Comment

          Working...