1

I'm having a problem with PHP where i'm writing to a file very often, and sometimes it takes a long time to open that file. The complete description is here:

fopen file locking in PHP (reader/writer type of situation)

My question is how can I get fopen to timeout in, say, 50ms. I looked at stream-context-create but that seems to be for HTTP, or at least, if it'll work for local files, I'm not sure how to specify the option in the array.

Any ideas?

Thank you!
Daniel

Community
  • 1
  • 1
Daniel Magliola
  • 30,898
  • 61
  • 164
  • 243
  • Artefacto's answer is valid - however PHP does not have sophisticated file handling semantics, nor the low-level OS access erquired to implement them. If it's at all possible, don't use files for storing your data in PHP – symcbean Sep 21 '10 at 11:42

2 Answers2

3

I'm not sure what you're trying here, but in some platforms (not Windows, though), you can open a file in non-blocking mode with the n flag:

$f = fopen("/tmp/foo/bar", "wn+");

This should return immediately. You can then probably use stream_select with a timeout of 50 ms.

I say "probably" because this flag is not documented.

Artefacto
  • 96,375
  • 17
  • 202
  • 225
-1

changing the default_socket_timeout variable in the php.ini to '1', would that helps?

René Höhle
  • 26,716
  • 22
  • 73
  • 82
Maik
  • 596
  • 1
  • 6
  • 16