1

Based on the code in this link http://webaudiodemos.appspot.com/AudioRecorder/index.html, I am making changes to send the recorded audio file to server by passing a sessionId via URL. The php page is http://xxxxx/abc.php?sessionId=Sam. PHP versions: PHP 5.4 PHP 5.5.22. I am using the 2nd method from this link:How to pass variables and data from PHP to JavaScript?. The abc.php page has reference to a few JS codes as with the index.html from the link above. abc.php page process the URL values correctly with the following code:

  <div id="bottom">
        <?php
            $faid = $_GET["sessionId"];
            echo htmlspecialchars($faid);     // tested working
        ?>
  </div>

On the recorder.js JavaScript,I have a function that tries to pass the URL values to another PHP while uploading the audio file to server - The fname is not being passed on ... it seems .. can the xhr.send(blob) will still send the fname?

  Recorder.setupDownload = function(blob){
    var div = document.getElementById("bottom"); 
    var fname = div.textContent;                 

  var xhr = new XMLHttpRequest();
  xhr.open('POST', "./uploads.php?" + fname, true); 
  xhr.onload = function(e) {};
  // Listen to the upload progress.
  var progressBar = document.querySelector('progress');
  xhr.upload.onprogress = function(e) {
    if (e.lengthComputable) {
      progressBar.value = (e.loaded / e.total) * 100;
      progressBar.textContent = progressBar.value; // Fallback for unsupported browsers.
    }
  };

  xhr.send(blob); 

The uploads.php in the server has the following script to receive the value and to create an audio file - but it is not creating the audio file - however, if I fix the file name (eg: "filename123") it writes the audio file - so the issue is in passing the variable name - I am a newbie and I wonder what am I missing?:

    <?php
        ini_set("display_errors", true);
        error_reporting(E_ALL); 

    if(isset($_GET['fileId']) && !empty($_GET['fileId'])){
     $id = $_GET["fileId"];
    } 
        $fp = fopen( $id, 'wb' );   // writes the audio file 
        fwrite( $fp, $GLOBALS['HTTP_RAW_POST_DATA'] );   
        fclose( $fp );
    ?>

Update: It is working!

Community
  • 1
  • 1

1 Answers1

0

You didn't give your value a name, so you're passing a value that will appear as DIFFERENT key in every page.

e.g. each of your users will have something like

http://example.com?foo
http://example.com?bar

leading to $_GET['foo'] and $_GET['bar'] in PHP. But since foo/bar are some randomish value representing your session ID, you have no idea what that value will be. So... give it a name:

http://example.com?key=foo

Now you just do $key = $_GET['key'] and can always access your session value, no matter what value it really as - it'll always be assigned to the key.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Marc, I have "sessionId" in the URL being assigned to a value "Sam" - http://xxxxx/abc.php?sessionId=Sam. Did I miss something? – user4687194 Mar 30 '15 at 19:32
  • you have your ajax call doing `"./uploads.php?" + fname`. nowhere in there is `sessionId` or whatever else. just whatever this `fname` value is. If you want fileId in php, then you'd need `"uploads.php?" + fname + '&fileid=' + file` – Marc B Mar 30 '15 at 19:33
  • What is "file" here? I tried xhr.open('POST', "./uploads.php?" + '&fileId=' + file , true); but it is no longer sending the audio file either. – user4687194 Mar 30 '15 at 19:42
  • When I change it to xhr.open('POST', "./uploads.php?" + '&fileId=' + fname , true); it is sending the audio file but the filename of the audio file is not correct. So the issue is extracting the sessionId and passing it to the JS ...the two lines var div, and var fname are intended to pass the sessionId but it is not ... how can I fix it? – user4687194 Mar 30 '15 at 20:03
  • Why using JavaScript to get the information from the DOM not working ... var div = document.getElementById("bottom"); var fname = div.textContent; Not sure how can I test/debug these? – user4687194 Mar 30 '15 at 20:38
  • I tried another method to pass the value to JS - still did not work. So I am wondering the issue is really in xhr.open('POST', "./uploads.php?" + '&fileId=' + fname , true) ? – user4687194 Mar 30 '15 at 21:16