简体   繁体   中英

Warning move_uploaded_file(): Unable to move audio file in php

I have simple Record Wave script using Recorder.js

Now The problem is

  1. Record is ok
  2. lessen to My record is ok
  3. download Record file from blob is ok
  4. Upload The record file (Here is The problem )
  5. operating system ( windows 10 & appserv )

MY upload.php

    <?php
    print $_FILES["audio_data"]["tmp_name"]; //this will print out the received name, temp name, type, size, etc.
    //check if upload folder exist or not
    if(!is_dir("uploads")){
        $res = mkdir("uploads",0777); 
    }

    $size = $_FILES['audio_data']['size']; //the size in bytes
    $input = $_FILES['audio_data']['tmp_name']; //temporary name that PHP gave to the uploaded file
    $output = $_FILES['audio_data']['name'].".wav"; //letting the client control the filename is a rather bad idea
    //move the file from temp name to local folder using $output name
    move_uploaded_file($input, $output)

    ?>

Part of My js file

function createDownloadLink(blob) {

    var url = URL.createObjectURL(blob);
    var au = document.createElement('audio');
    var li = document.createElement('li');
    var link = document.createElement('a');

    //name of .wav file to use during upload and download (without extendion)
    var filename = new Date().toISOString();

    //add controls to the <audio> element
    au.controls = true;
    au.src = url;

    //save to disk link
    link.href = url;
    link.download = filename+".wav"; //download forces the browser to donwload the file using the  filename
    link.innerHTML = "Save to disk";

    //add the new audio element to li
    li.appendChild(au);

    //add the filename to the li
    li.appendChild(document.createTextNode(filename+".wav "))

    //add the save to disk link to li
    li.appendChild(link);

    //upload link
    var upload = document.createElement('a');
    upload.href="#";
    upload.innerHTML = "Upload";
    upload.addEventListener("click", function(event){
          var xhr=new XMLHttpRequest();
          xhr.onload=function(e) {
              if(this.readyState === 4) {
                  console.log("Server returned: ",e.target.responseText);
              }
          };
          var fd=new FormData();
          fd.append("audio_data",blob, filename);
          xhr.open("POST","upload.php",true);
          xhr.send(fd);
    })
    li.appendChild(document.createTextNode (" "))//add a space in between
    li.appendChild(upload)//add the upload link to li

    //add the li element to the ol
    recordingsList.appendChild(li);
}

When Click to upload > Get that error in console

<b>Warning</b>:  move_uploaded_file(2019-12-05T17:30:46.190Z.wav): failed to open stream: Invalid argument in <b>F:\AppServ\www\audio\upload.php</b> on line <b>12</b><br />
<br />
<b>Warning</b>:  move_uploaded_file(): Unable to move 'C:\Windows\Temp\php891B.tmp' to '2019-12-05T17:30:46.190Z.wav' in <b>F:\AppServ\www\audio\upload.php</b> on line <b>12</b><br />

You are using output directory as the filename,hence an error is thrown.

As your upload directory is uploads.

Just try to update this:

$output ="uploads/".$_FILES['audio_data']['name'].".wav";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM