简体   繁体   English

使用php和ajax将音频上传到文件中

[英]upload audio into a file using php and ajax

I have the following php code to open a folder ad upload audio file into it: 我使用以下php代码打开一个文件夹,将广告上传音频文件上传到其中:

<?php

if(!is_dir("upload")){
$res = mkdir("upload",0777); 
}

// pull the raw binary data from the POST array
$data = substr($_POST['bufferFile'], strpos($_POST['bufferFile'], ",") + 1);
//echo($data);
// decode it
$decodedData = base64_decode($data);
echo($decodedData);
//echo ($decodedData);
$filename = urldecode($_POST['fname']);
echo($filename);
// write the data out to the file
 $fp = fopen('upload/'.$filename, 'wb');
fwrite($fp, $decodedData);
fclose($fp);

?>

I'm having the following errors: 我遇到以下错误:

Warning: fopen(upload/audio_recording_2014-08-11T11:21:02.213Z.wav): failed to open stream: Invalid argument in C:\\wamp\\www\\JSSoundRecorder\\upload.php on line 19 警告:fopen(upload / audio_recording_2014-08-11T11:21:02.213Z.wav):无法打开流:第19行的C:\\ wamp \\ www \\ JSSoundRecorder \\ upload.php中的参数无效

Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\\wamp\\www\\JSSoundRecorder\\upload.php on line 20 警告:fwrite()期望参数1为资源,在第20行的C:\\ wamp \\ www \\ JSSoundRecorder \\ upload.php中给出布尔值

Warning: fclose() expects parameter 1 to be resource, boolean given in C:\\wamp\\www\\JSSoundRecorder\\upload.php on line 21 can please someone help me what is going wrong?? 警告:fclose()期望参数1为资源,在第21行的C:\\ wamp \\ www \\ JSSoundRecorder \\ upload.php中给定的布尔值可以请有人帮助我怎么了?

this is the javascript (ajax) function: 这是javascript(ajax)函数:

  var reader = new FileReader();
var bufferFile;
var fileName = 'audio_recording_' +  new Date().toISOString() + '.wav';

reader.onload = function (event) {

  bufferFile = event.target.result;

    bufferFile = dataURItoArrayBuffer(bufferFile);

    postData(function() {
        var fd = new FormData();
        fd.append('fname', fileName);
        fd.append('bufferFile', bufferFile);
        $.ajax({
            type: 'POST',
            url: 'upload.php',
            data: fd,
            processData: false,
            contentType: false,
            success: function (data) {
                console.log(data);
            /*  $.ajax({
                    type: 'POST',
                    url: 'readFile.php',
                    data: {
                        "fileName": fileName,
                        "bufferFile": bufferFile
                    },
                    success: function (data) {
                        //console.log(data);


                    }
                });*/
            }
        });
    });
    console.log("nevermind");

};
reader.readAsDataURL(blob);

I believe this line $fp = fopen('upload/'.$filename, 'wb'); 我相信这行$fp = fopen('upload/'.$filename, 'wb'); is producing an error. 产生错误。 The mode wb is incorrect. 模式wb不正确。

According to this link , fopen 根据此链接fopen

Returns a file pointer resource on success, or FALSE on error. 成功返回文件指针资源,错误返回FALSE

Your first warning 您的第一个警告

Warning: fopen(upload/audio_recording_2014-08-11T11:21:02.213Z.wav): failed to open stream: Invalid argument in C:\\wamp\\www\\JSSoundRecorder\\upload.php on line 19 警告:fopen(upload / audio_recording_2014-08-11T11:21:02.213Z.wav):无法打开流:第19行的C:\\ wamp \\ www \\ JSSoundRecorder \\ upload.php中的参数无效

results from the incorrect wb mode. 错误的wb模式导致的结果。

The second and third warnings 第二和第三警告

Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\\wamp\\www\\JSSoundRecorder\\upload.php on line 20 警告:fwrite()期望参数1为资源,在第20行的C:\\ wamp \\ www \\ JSSoundRecorder \\ upload.php中给出布尔值

Warning: fclose() expects parameter 1 to be resource, boolean given in C:\\wamp\\www\\JSSoundRecorder\\upload.php on line 21 警告:fclose()期望参数1为资源,在第21行的C:\\ wamp \\ www \\ JSSoundRecorder \\ upload.php中给出布尔值

are as a result of you assuming that fp is a resource. 是由于您假设fp是一种资源。 fp is actually a boolean because of the first warning. 由于第一个警告, fp实际上是boolean It actually has the value FALSE which is boolean . 实际上,它的值为FALSE ,它是boolean

Maybe you meant to use w+ rather than wb . 也许您打算使用w+而不是wb I hope it helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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