title: PHP - Read and write the same file hangs
body: <p>I'm trying to use FFMPEG to make some works with video on the server, and something I need to do is to get the progress of the process. </p>
  
  <p>I searched a little and I found <a href="https://stackoverflow.com/questions/11441517/ffmpeg-progress-bar-encoding-percentage-in-php">this solution</a> which tells to write the log into a file and then reading and parsing it.</p>
  
  <h1>The problem</h1>
  
  <p>What is driving me crazy is that I tell FFMPEG - with <code>exec</code> - (<em>process A</em>) to write the log into a file, but when I try to read it - with <code>file_get_contents()</code> - (<em>process B</em>) it does not show the contents until <em>process A</em> is finished (or interrupted the PHP script).</p>
  
  <p>So, when <em>process A</em> finishes or it says "PHP script timeout", then I can read the file as times as I want, refreshing the page (<em>process B</em>) and showing the contents at the time.</p>
  
  <h1>What I've tried</h1>
  
  <p>I've tried to use <code>fopen()</code> to create the file with <code>w</code>, <code>w+</code> and <code>a</code> parameters, using - and without using - <code>fclose()</code>. I've tried to use also <code>flock()</code> just in case it gets faster to read to <em>process B</em> if it knows it's already locked and does not have to wait, but then FFMPEG is not able to write into the file.</p>
  
  <p>I've searched for <a href="https://stackoverflow.com/questions/70855/how-can-one-use-multi-threading-in-php-applications">multithreading</a> too, but I think there must be an easier and simpler way.</p>
  
  <p>I've used also CURL and HTTP context, as <a href="https://stackoverflow.com/questions/3629504/php-file-get-contents-very-slow-when-using-full-url">this link</a> suggests, but no luck.</p>
  
  <p>I've tried, too, to use PHP-FFMPEG but it's not supporting the last FFMPEG version, so I cannot use it.</p>
  
  <p><em>When I said before "(or interrupted the PHP script)" is because I tried to wait and, when PHP got a timeout,</em> process B <em>worked alright and the file was still updating.</em></p>
  
  <h1>The code</h1>
  
  <h2>Process A (fileA.php)</h2>
  
  <p><code>exec('ffmpeg -y -i input_file.mp4 output_file.avi 2&gt; C:\Full\Path\To\File\log.txt 1&gt;&amp;2');</code></p>
  
  <h2>Process B (fileB.php)</h2>
  
  <pre><code>$content = file_get_contents($file);
  
  if($content){
      //get duration of source
      preg_match("/Duration: (.*?), start:/", $content, $matches);
  
      $rawDuration = $matches[1];
  
      //rawDuration is in 00:00:00.00 format. This converts it to seconds.
      $ar = array_reverse(explode(":", $rawDuration));
      $duration = floatval($ar[0]);
      if (!empty($ar[1])) $duration += intval($ar[1]) * 60;
      if (!empty($ar[2])) $duration += intval($ar[2]) * 60 * 60;
  
      //get the time in the file that is already encoded
      preg_match_all("/time=(.*?) bitrate/", $content, $matches);
  
      $rawTime = array_pop($matches);
  
      //this is needed if there is more than one match
      if (is_array($rawTime)){$rawTime = array_pop($rawTime);}
  
      //rawTime is in 00:00:00.00 format. This converts it to seconds.
      $ar = array_reverse(explode(":", $rawTime));
      $time = floatval($ar[0]);
      if (!empty($ar[1])) $time += intval($ar[1]) * 60;
      if (!empty($ar[2])) $time += intval($ar[2]) * 60 * 60;
  
      //calculate the progress
      $progress = round(($time/$duration) * 100);
  
      echo "Duration: " . $duration . "&lt;br&gt;";
      echo "Current Time: " . $time . "&lt;br&gt;";
      echo "Progress: " . $progress . "%";
  
  }
  </code></pre>
  
  <h2>The process</h2>
  
  <p>I just open <code>fileA.php</code> on a Chrome tab and, after a few seconds, I open <code>fileB.php</code> on another Chrome tab (and it stays as <em>loading</em>).</p>
  
  <h2>What I need</h2>
  
  <p>I need to be able to load the file and show the information I want to show while the file is being written (by <code>exec</code> and <code>FFMPEG</code> or other PHP scripts), so I can update the progress percentage with some AJAX calls.</p>
  
  <h2>Extra information</h2>
  
  <p>At this point, I'm using PHP 5.4 on a IIS 7.5 with Windows 7 Professional.</p>
  
  <p>Thank you everyone for your time, help and patience!</p>
  
  <p>Best regards.</p>