简体   繁体   中英

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

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

Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\\wamp\\www\\JSSoundRecorder\\upload.php on line 20

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

this is the javascript (ajax) function:

  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'); is producing an error. The mode wb is incorrect.

According to this link , fopen

Returns a file pointer resource on success, or FALSE on error.

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

results from the incorrect wb mode.

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

Warning: fclose() expects parameter 1 to be resource, boolean given in C:\\wamp\\www\\JSSoundRecorder\\upload.php on line 21

are as a result of you assuming that fp is a resource. fp is actually a boolean because of the first warning. It actually has the value FALSE which is boolean .

Maybe you meant to use w+ rather than wb . I hope it helps.

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