title: Send Recorded Audio File to Web Server With PHP
body: <p>I'm using <a href="https://github.com/mattdiamond/Recorderjs" rel="noreferrer">Recorder.js</a> which allows users to create their own sound recording from their microphone input. I'm new to this so I'm using <a href="http://webaudiodemos.appspot.com/AudioRecorder/index.html" rel="noreferrer">this site</a> as a point of reference.</p>
  
  <p>This code creates a downloadable .wav file:
  </p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;script src="js/recorderjs/recorder.js"&gt;&lt;/script&gt;
          &lt;script src="js/main.js"&gt;&lt;/script&gt;
          &lt;script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;
      &lt;/head&gt;
  
      &lt;body&gt;
          &lt;img id="record" src="mic.png" onclick="toggleRecording(this);"&gt;&lt;br&gt;
  
          &lt;img src="save.svg" onclick="saveAudio();"&gt;
  
          &lt;form action="savefile.php" method="post"&gt;
              &lt;input type="submit" value="Submit"&gt;
          &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This starts and stops the recording:</p>
  
  <pre><code>&lt;img id="record" src="mic.png" onclick="toggleRecording(this);"&gt;
  </code></pre>
  
  <p>And this downloads the .wav file from the recorder.js script:</p>
  
  <pre><code>&lt;img src="save.svg" onclick="saveAudio();"&gt;
  </code></pre>
  
  <p>I'm using this PHP script to try and save the .wav file to the /audio directory:</p>
  
  <pre><code>&lt;?php
  
  $save_folder = dirname(__FILE__) . "/audio";
  if(! file_exists($save_folder)) {
      if(! mkdir($save_folder)) {
          die("failed to create save folder $save_folder");
      }
  }
  
  $key = 'filename';
  $tmp_name = $_FILES["audiofile"]["tmp_name"];
  $upload_name = $_FILES["audiofile"]["name"];
  $type = $_FILES["audiofile"]["type"];
  $filename = "$save_folder/$upload_name";
  $saved = 0;
  if(($type == 'audio/x-wav' || $type == 'application/octet-stream') &amp;&amp; preg_match('/^[a-zA-Z0-9_\-]+\.wav$/', $upload_name) ) {
  
      $saved = move_uploaded_file($tmp_name, $filename) ? 1 : 0;
  }
  
  ?&gt;
  </code></pre>
  
  <p>Now, what I'm hoping for is someone to help me with a PHP script which uploads the .wav files to the folder /audio instead of having the user download the file.</p>
  
  <p>Any help with this would be greatly appreciated.</p>