1

What's the best way, idiomatically, of getting the file contents (into a string) of a flock'd file in PHP?

Edit: To be clear, this is something locked by the current page/process. Something like:

if(flock($fp ...)) {
  $str = READFILECONTENTS($fp);
  flock(...);
}
Aaron Yodaiken
  • 19,163
  • 32
  • 103
  • 184
  • 3
    flock() is is advisory. Simply open the file in read mode without checking the lock. However, if there may be ongoing writes to the file, this isn't a very good idea. – Frank Farmer May 13 '11 at 01:10
  • It's only advisory on some platforms. – Aaron Yodaiken May 13 '11 at 01:17
  • possible duplicate of [PHP check if file locked with flock()?](http://stackoverflow.com/questions/3149324/php-check-if-file-locked-with-flock) – Alix Axel May 13 '11 at 01:23
  • A quick scan of the wikipedia article on file locking suggests that on windows, you can copy a locked file and read the copy. Other than that, utilities that "unlock" locked files apparently do so by revoking the lock, or killing the process holding the lock -- neither of which really allow you to read a locked file; instead they unlock the file so you can read the unlocked file. The idea of reading a file locked with a mandatory lock flies in the face of the concept of a mandatory lock... – Frank Farmer May 13 '11 at 01:25

1 Answers1

1

Don't. Wait for the lock to be released and only then read it.

Or just read it anyway.

Alix Axel
  • 151,645
  • 95
  • 393
  • 500