title: PHP, check if the file is being written to/updated by PHP script?
body: <p>I have a script that re-writes a file every few hours. This file is inserted into end users html, via php include.</p>
  
  <p>How can I check if my script, at this exact moment, is working (e.g. re-writing) the file when it is being called to user for display? Is it even an issue, in terms of what will happen if they access the file at the same time, what are the odds and will the user just have to wait untill the script is finished its work?</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>More on the subject...</strong></p>
  
  <p>Is this a way forward using <code>file_put_contents</code> and <code>LOCK_EX</code>?</p>
  
  <p>when script saves its data every now and then</p>
  
  <pre><code>file_put_contents($content,"text", LOCK_EX);
  </code></pre>
  
  <p>and when user opens the page</p>
  
  <pre><code>if (file_exists("text")) {
  
  function include_file() {
  
      $file = fopen("text", "r");
      if (flock($file, LOCK_EX)) {
      include_file();
      }
      else {
      echo file_get_contents("text");
      }
  }
   } else {
  echo 'no such file';
  }
  </code></pre>
  
  <p>Could anyone advice me on the syntax, is this a proper way to call <code>include_file()</code> after condition and how can I limit a number of such calls?</p>
  
  <p>I guess this solution is also good, except same call to <code>include_file()</code>, would it even work?</p>
  
  <pre><code>function include_file() {
  
  $time = time();
  $file = filectime("text");
  
  if ($file + 1 &lt; $time) {
  echo "good to read";
  } else {
  echo "have to wait";
  include_file();
  }
  
  }
  </code></pre>