javascript - 警告 move_uploaded_file() : Unable to move audio file in php

标签 javascript php ajax

我有使用 Recorder.js 的简单 Record Wave 脚本

现在的问题是

  1. 记录正常
  2. 减少到我的记录没问题
  3. 可以从 blob 下载记录文件
  4. 上传记录文件(这是问题所在)
  5. 操作系统(Windows 10 和 appserv)
<小时/>

我的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)

    ?>

我的js文件的一部分

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

当点击上传>在控制台中出现该错误

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

最佳答案

您正在使用输出目录作为文件名,因此会引发错误。

因为你的上传目录是uploads。

尝试更新一下:

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

关于javascript - 警告 move_uploaded_file() : Unable to move audio file in php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59200609/

相关文章:

jquery - Ajax for 循环中将变量 i 值递增到总长度

javascript - 使用 Ajax 创建 session 并回显结果

javascript - Jquery 替换嵌套标签内的文本

php - 图像源在 Laravel 中不可读

php - 如何检测域是否已捕获所有接受电子邮件的策略?

javascript - 防止刷新时调用 onbeforeunload()

javascript - 将带有ajax请求的数组发送到php

javascript - 单元测试 Vue 组件 - Jest 设置 - 意外的标识符错误

javascript - 点击浏览器中的一个按钮,该按钮会取消浏览器的焦点,然后聚焦到另一个应用程序?

javascript - React 中使用 FileReader 的正确获取主体是什么