我正在使用recorder.js
库,并希望使用PHPMailer
将记录的消息发送到我的Gmail帐户。我已经做了所有事情,但是唯一的问题是,当我将文件作为附件发送并从邮件中下载时,文件已损坏(或其他原因),并且系统显示"The file is unplayable"
。此外,当我检查写入所有文件的本地uploads/
文件夹时,它们也无法播放。我不知道这可能是什么问题,并且自从过去两天以来,我一直坚持下去。预先感谢。
我对upload.php
的JS调用
function sendMessage() {
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);
}
和我的upload.php
<?php
require "php-mailer-master/PHPMailerAutoload.php";
define('UPLOAD_DIR', 'uploads/');
$a = $_FILES['audio_data']['name'];
$a = str_replace('data:audio/wav;base64,', '', $a);
$a = str_replace(' ', '+', $a);
$data = base64_decode($a);
$file = UPLOAD_DIR . uniqid() . '.wav';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
请注意,由于我认为这无关紧要,因此我已经跳过了实际发送邮件的代码部分。