title: PHP - fread() waiting until another fwrite() script ends
body: <h2>Update</h2>
  
  <p>As <a href="https://stackoverflow.com/users/561309/this-lau">this.lau_</a> commented, it seems the problem is that no code is running at all on the server when the first file is running. I guess some configuration only allows one script to be run at once - or only one script per "user".</p>
  
  <p>Now I will investigate what can be causing that behaviour on the server. Thank you everyone for your help!</p>
  
  <p>.</p>
  
  <h2>Original question</h2>
  
  <p>I've got two PHP scripts, on separate files:</p>
  
  <pre><code>/* The first file contains */
  for($i = 0; $i &lt; 10000; $i++){
      $w = fopen($progress_file, "w");
      fwrite($w, $i);
      fclose($w);
  }
  
  /* The second file contains */
  $f = fopen($progress_file, "r");
  $progress_read = fread($f, filesize($progress_file));
  fclose($f);
  echo $progress_read !== false ? $progress_read : 0;
  </code></pre>
  
  <p>As you can see, I make a loop so I have time to refresh second file and wait for it to give the the number it's written in that moment.</p>
  
  <p>I know that, when doing <code>fopen($file, "w");</code> I'm truncating that file, so it won't give me (sometimes) the number it should give, and it will give 0. However, <strong>it should echo something</strong>, as I check if the file has been able to be read and, otherwise, echo "0".</p>
  
  <p>Instead, I get the second file waiting (shown as loading) without echoing nothing until the first file gets completed the whole loop (so I always end up with "9999" on the second file).</p>
  
  <p>I've tried changing the modes of <code>fopen()</code> with no luck, as well as making the <code>fopen()</code> and <code>fclose()</code> outside the loop. No luck neither. I've also tried changing the fopen-fread-fclose thing with <code>file_get_contents()</code> and other functions and nothing has worked.</p>
  
  <p>Also, <code>fflush()</code>, <code>ob_flush()</code> and <code>flush()</code> haven't made any difference.</p>
  
  <p>As I said, the file gets written (if I open it with a text editor, I see the content changes) and even if it wouldn't, it should echo "0".</p>
  
  <p><strong>Why is the second file waiting, without echoing nothing, until the first loop ends?</strong></p>
  
  <p>Thank you.</p>