无法将文件上传到服务器页面

时间:2018-12-19 14:53:37

标签: javascript php record

当按下上传链接时,我无法在服务器页面上检索音频文件。您的任何帮助将不胜感激。 这是要上传的javascript代码:

//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);

这是php代码:

<?php
  print_r($_FILES); //this will print out the received name, temp name, type, size, etc.
  $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)
?>

发生这种类型的错误: Error

1 个答案:

答案 0 :(得分:0)

为输出指定完整的目标路径,而不仅仅是文件名。

$output = "C:\temp\".$_FILES['audio_data']['name'].".wav";

有时,您还需要正斜杠而不是反斜杠。并且要注意安全问题,浏览器提供的文件名可能包含baaad内容。