title: Video File Upload Progress Bar with Javascript and PHP
body: <p>I want to upload a video file using PHP and show the progress of the upload via an Progress Bar. But this is more difficult like i thought and i tried to put the pieces ive found together but unfortunately i didnt found a working piece of code that has the needed php, ajax and html code all together, so ive tried to put different pieces together.</p>
  
  <p>My Code functions nearly completely. The only thing is, that the current process of the file upload, which i've got in percent, is loaded by my javascript only AFTER the process has ended, and not from the beginning.</p>
  
  <p>Here is my PHP Code:</p>
  
  <pre><code> function file_get_size($file) {
      //open file
      $fh = fopen($file, "r");
      //declare some variables
      $size = "0";
      $char = "";
      //set file pointer to 0; I'm a little bit paranoid, you can remove this
      fseek($fh, 0, SEEK_SET);
      //set multiplicator to zero
      $count = 0;
      while (true) {
          //jump 1 MB forward in file
          fseek($fh, 1048576, SEEK_CUR);
          //check if we actually left the file
          if (($char = fgetc($fh)) !== false) {
              //if not, go on
              $count ++;
          } else {
              //else jump back where we were before leaving and exit loop
              fseek($fh, -1048576, SEEK_CUR);
              break;
          }
      }
      //we could make $count jumps, so the file is at least $count * 1.000001 MB large
      //1048577 because we jump 1 MB and fgetc goes 1 B forward too
      $size = bcmul("1048577", $count);
      //now count the last few bytes; they're always less than 1048576 so it's quite fast
      $fine = 0;
      while(false !== ($char = fgetc($fh))) {
          $fine ++;
      }
      //and add them
      $size = bcadd($size, $fine);
      fclose($fh);
      return $size;
  }
  $filesize = file_get_size('remote-file');
  
  $remote = fopen('remote-file', 'r');
  $local = fopen('local-file', 'w');
  
  $read_bytes = 0;
  while(!feof($remote)) {
    $buffer = fread($remote, 2048);
    fwrite($local, $buffer);
  
    $read_bytes += 2048;
  
    //Use $filesize as calculated earlier to get the progress percentage
    $progress = min(100, 100 * $read_bytes / $filesize);
    fwrite(fopen('files/upload/progress.txt', 'w'), $progress);
  
    //you'll need some way to send $progress to the browser.
    //maybe save it to a file and then let an Ajax call check it?
  }
  fclose($remote);
  fclose($local);
  </code></pre>
  
  <p>This is my Javascript Code:</p>
  
  <pre><code>function main()
  {
      var pathOfFileToRead = "files/upload/progress.txt";
  
      var contentsOfFileAsString = FileHelper.readStringFromFileAtPath
      (
          pathOfFileToRead
      );
  
      document.body.innerHTML = contentsOfFileAsString;
  }
  
  function FileHelper()
  {}
  {
      FileHelper.readStringFromFileAtPath = function(pathOfFileToReadFrom)
      {
          var request = new XMLHttpRequest();
          request.open("GET", pathOfFileToReadFrom, false);
          request.send(null);
          var returnValue = request.responseText;
  
          return returnValue;
      }
  }
  
  main();
  
  function progressBarSim(al) {
    var bar = document.getElementById('bar-fill');
    var status = document.getElementById('status');
    status.innerHTML = al+"%";
    bar.value = al;
    al++;
  var sim = setTimeout("progressBarSim("+al+")",1000);
  if(al == 100){
   status.innerHTML = "100%";
   bar.value = 100;
   clearTimeout(sim);
   var finalMessage = document.getElementById('finalMessage');
   finalMessage.innerHTML = "Process is complete";
  }
  }
  var amountLoaded = 0;
  progressBarSim(amountLoaded);
  </code></pre>
  
  <p>The Progressbar does currently work over an Timer, because the main() function doesnt read the content of the "progress.txt" from the beginning but only at the end. so i would like to have some help to combine progressBarSim with main().</p>
  
  <p>*<em>Edit: *</em> I have found a working piece of code: <a href="http://www.it-gecko.de/html5-file-upload-fortschrittanzeige-progressbar.html" rel="nofollow">http://www.it-gecko.de/html5-file-upload-fortschrittanzeige-progressbar.html</a> and am using that now.</p>