发送录制的音频文件到Web服务器与PHP [英] Send Recorded Audio File to Web Server With PHP
问题描述
我使用 Recorder.js 它允许用户从他们的麦克风输入创建自己的录音。我是新来这个,所以我使用本网站作为一个参考点。
I'm using Recorder.js which allows users to create their own sound recording from their microphone input. I'm new to this so I'm using this site as a point of reference.
这code创建一个可下载的.wav文件:
This code creates a downloadable .wav file:
<html>
<head>
<script src="js/recorderjs/recorder.js"></script>
<script src="js/main.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<img id="record" src="mic.png" onclick="toggleRecording(this);"><br>
<img src="save.svg" onclick="saveAudio();">
<form action="savefile.php" method="post">
<input type="submit" value="Submit">
</form>
</body>
</html>
此启动和停止录音:
<img id="record" src="mic.png" onclick="toggleRecording(this);">
和下载这从recorder.js脚本.wav文件:
And this downloads the .wav file from the recorder.js script:
<img src="save.svg" onclick="saveAudio();">
我使用这个PHP脚本来尝试和.wav文件保存到/ audio目录:
I'm using this PHP script to try and save the .wav file to the /audio directory:
<?php
$save_folder = dirname(__FILE__) . "/audio";
if(! file_exists($save_folder)) {
if(! mkdir($save_folder)) {
die("failed to create save folder $save_folder");
}
}
$key = 'filename';
$tmp_name = $_FILES["audiofile"]["tmp_name"];
$upload_name = $_FILES["audiofile"]["name"];
$type = $_FILES["audiofile"]["type"];
$filename = "$save_folder/$upload_name";
$saved = 0;
if(($type == 'audio/x-wav' || $type == 'application/octet-stream') && preg_match('/^[a-zA-Z0-9_\-]+\.wav$/', $upload_name) ) {
$saved = move_uploaded_file($tmp_name, $filename) ? 1 : 0;
}
?>
现在,正是我希望的是有人帮我一个PHP脚本上传.wav文件的文件夹/音频而不是让用户下载的文件。
Now, what I'm hoping for is someone to help me with a PHP script which uploads the .wav files to the folder /audio instead of having the user download the file.
任何帮助,这将大大AP preciated。
Any help with this would be greatly appreciated.
推荐答案
我觉得你不能使用usuall HTTP文件中的字段,因为他们期望的本地路径。
你可以尝试用一个AJAX上传
I think you can't use usuall http file fields, because they expect a local path. You could try a upload with AJAX.
var req = null;
var url = "savefile.php";
var data = "": // you have to check how to get the data from your saveAudio() method
(window.XMLHttpRequest) ? req = new XMLHttpRequest() : (window.ActiveXObject) ? req = new ActiveXObject("Microsoft.XMLHTTP") : req = false;
req.open(method, url, true);
req.setRequestHeader("Content-Type", "multipart/form-data");
if(data != null && data != "")
{
req.setRequestHeader("Content-length", data.length);
req.send(data);
}
您yust需要找出如何从你的 saveAudio()
函数来获取filecontent,并填写到数据
变量。
You yust need to find out how you can get the filecontent from your saveAudio()
function and fill it into the data
variable.
也许这可以帮助你太使用Ajax上传文件
这篇关于发送录制的音频文件到Web服务器与PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!